[英]How to push a child into Firebase RealTime Database with a specified key?
我正在使用React Native和Firebase RealTime数据库开发一个应用程序。
我要执行以下操作。 我想创建如下的数据树。
myRoot
|
|---myKey: "myValue"
但是在这里,我不想这样做。
foo = async () => {
let myRef = firebase.database().ref('myRoot');
await myRef.set({
myKey: "myValue"
})
}
相反,我想做这样的事情。
writeData = async (key, value) => {
let myRef = firebase.database().ref('myRoot');
await myRef.set({
key: value
})
}
并且,然后调用该函数以添加数据。
this.writeData(myKey, myValue);
例如,在我如下调用函数之后,
this.writeData("myFirstKey", "myFirstValue");
this.writeData("mySecondKey", "mySecondValue");
this.writeData("myThirdKey", "myThirdValue");
我需要以下输出。
myRoot
|
|---myFirstKey: "myFirstValue"
|---mySecondKey: "mySecondValue"
|---myThirdKey: "myThirdValue"
这个怎么做?
在给定对象上调用set将完全替换它,而不是set,您可以使用像这样的更新功能
writeData = async (key, value) => {
let myRef = firebase.database().ref('myRoot');
await myRef.update({
key:value
})
}
是的..我明白了..
writeData = async (key, value) => {
let myRef = firebase.database().ref('myRoot');
await myRef.child(key).set(value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.