Fork me on GitHub
#clojurescript
<
2019-10-16
>
itaied06:10:02

I have a strange bug in my re-frame app. Simply put, these lines

(.log js/console user)
   (println (get user "displayName" "anonynous"))
print the following:
subs.cljs:16 {... displayName: "John Doe"}
core.cljs:192 anonynous
The user does contain a key named displayName, but when I log it it can't find it. I can put here all of the events and effects of re-frame, but the problem is right there

p-himik06:10:29

It seems that user is a regular JS object. Try using (.-displayName user) or goog.object/get.

itaied06:10:44

it did the trick, thanks again 🙂

itaied06:10:18

what is the best practice when combining js and clj? I get stuck at it all the time.. should I transform js object to clj? or use the goog lib?

p-himik06:10:36

It depends no the nature of that object. If it's data, I thik you should use either goog.object/get or cljs-oops. If it's some instance of some type, you should probably use externs inference and regular JS interop, especially if you use shadow-cljs. I keep using cljs-oops for the latter as well though.

p-himik06:10:55

In general, you can't use (.-displayName user) for neither data nor code because it probably won't work in production where GCC is run with :advanced optimizations.

itaied07:10:47

cant i transform js object (data) to cljs data? I thought that was the point of js->clj

p-himik07:10:30

You sure can! And I think there are libraries out there that claim to do it faster than js->clj if you care about it. But you should do it only if you need the complete data. If you just need a single field, there's no point in converting the whole data. Also, js->clj will not work for non-data objects (i.e. where (not (identical? (type x) js/Object)).

itaied07:10:44

great, thanks a lot 🙂

p-himik07:10:39

No problem.

thheller09:10:48

when working with data is is advised to use goog.object/get

thheller09:10:14

js->clj works but is rather slow. prefer something like cljs-bean instead.