I have an object that I want to push to the firebase realtime database, looking like this:
userProfile = { name: "Jon", age: 25, gender: "male", ...plenty more attributes}
If I want it in firebase, I could write it like this:
return firebase.database().ref('/userProfile').push({
name: userProfile.name,
age: userProfile.age,
gender: userProfile.gender,
....
}).
But since I have plenty of objects with many attributes I would prefer not writing it by hand. Loops are not allowed in push (). I could push the whole object like this:
return firebase.database().ref('/userProfile').push({
userProfile: userProfile
}).
But it will create an additional child node like
ref/userProfile/123someId/userProfile/name
which is bad practise because it disallows using filters and more later on.
Is there a more effective way to push the attributes of an entire object without writing down every key/value pair?
I have an object that I want to push to the firebase realtime database, looking like this:
userProfile = { name: "Jon", age: 25, gender: "male", ...plenty more attributes}
If I want it in firebase, I could write it like this:
return firebase.database().ref('/userProfile').push({
name: userProfile.name,
age: userProfile.age,
gender: userProfile.gender,
....
}).
But since I have plenty of objects with many attributes I would prefer not writing it by hand. Loops are not allowed in push (). I could push the whole object like this:
return firebase.database().ref('/userProfile').push({
userProfile: userProfile
}).
But it will create an additional child node like
ref/userProfile/123someId/userProfile/name
which is bad practise because it disallows using filters and more later on.
Is there a more effective way to push the attributes of an entire object without writing down every key/value pair?
Share Improve this question edited Sep 5, 2016 at 20:55 Frank van Puffelen 600k85 gold badges890 silver badges860 bronze badges asked Sep 5, 2016 at 20:49 ChrisChris 4,4067 gold badges29 silver badges51 bronze badges 3-
1
Can you just pass
userProfile
? (e.g.return firebase.database().ref('/userProfile').push(userProfile)
) – Rob M. Commented Sep 5, 2016 at 20:51 -
Not sure if I understand you, but if you don't want the extra
userProfile
level:firebase.database().ref('/userProfile').push(userProfile)
– Frank van Puffelen Commented Sep 5, 2016 at 20:56 - Ah, you guys just saved my day. Yes that works, thanks a lot. – Chris Commented Sep 5, 2016 at 21:08
1 Answer
Reset to default 8The answer could not be easier, but in case someone else stumbles across the same issue:
firebase.database().ref('/userProfile').push(userProfile)
Thanks guys