将对象推送到Firebase的智能方法 [英] Smart way to push object to firebase
问题描述
我有一个要推送到Firebase实时数据库的对象,如下所示:
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}
如果我想在firebase中使用它,可以这样写:
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,
....
}).
但是由于我有许多具有许多属性的对象,所以我宁愿不手工编写它. push()中不允许循环.我可以像这样推动整个对象:
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?
推荐答案
答案并非易事,但如果有人偶然发现同一问题:
The answer could not be easier, but in case someone else stumbles across the same issue:
firebase.database().ref('/userProfile').push(userProfile)
谢谢大家
这篇关于将对象推送到Firebase的智能方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!