Fork me on GitHub

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


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


it did the trick, thanks again 🙂


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?


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.


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.


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


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)).


great, thanks a lot 🙂


No problem.


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


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