javascript - How to push a child into Firebase RealTime Database with a specified key?
问题描述
I'm developing an app using React Native and Firebase RealTime database.
I want to do the following. I want to create a data tree like following.
myRoot
|
|---myKey: "myValue"
But here, I DON'T want to do it as follows.
foo = async () => {
let myRef = firebase.database().ref('myRoot');
await myRef.set({
myKey: "myValue"
})
}
Instead, I want to do something like this.
writeData = async (key, value) => {
let myRef = firebase.database().ref('myRoot');
await myRef.set({
key: value
})
}
And, then call the function to add data.
this.writeData(myKey, myValue);
For example, after I called the function as below,
this.writeData("myFirstKey", "myFirstValue");
this.writeData("mySecondKey", "mySecondValue");
this.writeData("myThirdKey", "myThirdValue");
I need the output as below.
myRoot
|
|---myFirstKey: "myFirstValue"
|---mySecondKey: "mySecondValue"
|---myThirdKey: "myThirdValue"
How to do this?
解决方案
Calling set on a given object will replace it completely instead of set you can use update function like this
writeData = async (key, value) => {
let myRef = firebase.database().ref('myRoot');
await myRef.update({
key:value
})
}
推荐阅读
- android - 更改字段文本的 PDFbox 字体
- android - 如何在 react-native 中添加 box-shadow?
- python - tkinter 标签中表格结构的 Tkinter 问题
- node.js - MongoDB - 批量查找和 updateOne 无法按预期工作
- javascript - 使用 EJS 的骰子滚轮
- php - 如何在POST方法的下一个php页面上接收从标题(位置:.....)发送的数据?
- python - 使用 Python 仅从 txt 文件中提取所需的列?
- ios - 如何将 AppiumDriver 焦点更改为出现在不同图层上的下拉菜单?iOS
- javascript - JQuery在页面上输出“未定义”
- javascript - Jquery Validation:在提交时验证一个字段一次