Fork me on GitHub
#clojurescript
<
2022-07-27
>
Hankstenberg06:07:38

Hi guys, I have a probably dumb question. I have a js object created with the "new" constuctor and now I want to set a property of it using the "set" function. I'm creating the object like this: (def f1 (new fdn.FormData)) now I'd like to invode the "set" function with two arguments. I tried all kinds of things. My most recent attempt was: (def f2 (goog.object/set f1 "grant_type" "client_credentials")) I also tried: (def f2 (set! f1 "grant_type=client_credentials")) but this seems to replace f1 with a string "grant_type=client_credentials" Any help would be appreciated

thheller06:07:57

@roseneck do you want to do f1.set("grant_type", "client_credentials") or f1.grant_type = "client_credentials";?

thheller06:07:20

(.set f1 "grant_type" "client_credentials")

thheller06:07:15

the . is for calling interop functions on an object. first argument is the object, the rest are the arguments

Hankstenberg06:07:39

I tried that but when I run: (def f1 (new fdn.FormData)) (def f2 (.set f1 "grant_type" "client_credentials")) f2 then nil is returned

thheller06:07:59

that is fine, it is mutating f1

Hankstenberg06:07:26

D'oh... thank you very much!

p-himik06:07:55

I'd write it as

(def f1 (doto (fdn.FormData.)
          (.set "grant_type" "client_credentials")))
But having a separate .set expression works just fine.

👍 1
Oliver Marks18:07:53

any one know how to resolve this Cannot infer target type in expression (. state -key) I can not find any info on .-key or where it comes from I knwo I need to give it a hint to make the error go away just not sure what to tag it with 😕

thheller19:07:03

^js state instead of state for example

Oliver Marks19:07:27

Thanks you so much for that it was my finaly infer target error, been ignoring it for a while I was convinced it would not be needed in front of state because its an reagent atom rather than a native js type

Oliver Marks19:07:55

I tried it in other places just not there, I would still like to know where .-key comes from as my google search brings up nothing and cider can't jump to it, figure if I could have found reference to it might have made sense 🙂

thheller19:07:06

you mean its not in your own code?

thheller19:07:17

I mean the error tells you where it is coming from? meaning it should point to the error location?

Oliver Marks19:07:43

I am using it in my code but .-key is not something I know anything about, I have seen it used to detect datascript db changes which is where I got the example, but I have no idea what its actually doing or how to find out

Oliver Marks20:07:26

this gist has an example usage https://gist.github.com/allgress/11348685 but its not hinted and does not help me know what the code is doing

thheller20:07:41

.-__key is just accessing the __key property on a JS object. so obj.__key

thheller20:07:57

I guess you are using shadow-cljs which turns own externs inference warnings by default

thheller20:07:06

whoever wrote that file probably didn't and thus didn't see the warning

thheller20:07:50

you can either add the typehint or add (set! *warn-on-infer* false) after the ns form

thheller20:07:55

that'll also make it shutup

Oliver Marks20:07:57

yeah the error has gone now, the .- is a gap in my knowledge I guess I googled -key but I guess .- is part of the clojurescript which I have forgotten or not come across

Oliver Marks20:07:57

okay thanks found the relavant docs on the subject its property access on the js object

Hankstenberg19:07:20

I'm having a kind of embarrassing problem. I'm working with promesa on cljs for the first time and I'm really scratching my head over the question of how I can access the resolved values of promises. Considering a call like this:

(defn post [url headers data]
  (p/let [response-post (-> got (.post url (clj->js {:headers headers :json data})) .json)]
    (println (js->clj response-post :keywordize-keys true))))
I can print the response alright, but I'd actually like to return the resolved value. But how can I make this work? The only way to do it is to reset! an atom, but that can't be right, can it?

thheller19:07:29

JS is mostly async when it comes to this sort of stuff

thheller19:07:42

and you simply cannot access async results in a sync fashion

thheller19:07:56

so that is something you need to get used to when developing CLJS/JS apps 🙂