degree9 / firebase-cljs

Another set of Firebase CLJS bindings.
MIT License
26 stars 4 forks source link

clj->js into firebase, then js->clj out is not the same #26

Open Folcon opened 7 years ago

Folcon commented 7 years ago

@flyboarder, been working with this for a bit now and it's going pretty well!

I have noticed an odd issue, it seems that I get different values sending data into the firebase db vs pulling data out.

data goes in:

#js {:items #js {:1 #js {:id 1, :date 1489930651500}, :2 #js {:id 2, :date 1489930651652}}}

data comes out:

{items [nil {date 1489930651500, id 1} {date 1489930651652, id 2}]}

I'm doing:

(fbdb/listen @*ref* "value" #(println (js->clj (fbsnap/val %))))

It's frustrating as the correct values appear to be being stored in firebase, but the keys in the hash-maps seem to be repeatedly dropped. It seems to be pulling out objects as arrays?

I've tried js->clj with and without keywordize-keys and it doesn't seem to make any difference.

Is this an actual bug, or am I just doing something silly?

Folcon commented 7 years ago

Ok, so having done a little more digging, it seems to be a bug in firebase?

As far as I can work out if you use int keys, it always comes out as an array. So my solution has been to add "key-" as a prefix to my int keys going in, and remove them going out.

Tested what's being returned using it as a double check:

var uid = firebase.database().app.auth().currentUser.uid;
firebase.database().ref('users/' + uid).on('value', function(snapshot) {
    console.log("FROM FIREBASE:", snapshot.val());
});

Maybe worth documenting this?

burn2delete commented 7 years ago

This is actually correct firebase behavior. https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html?m=1