我在想,有没有办法更新关键值?
我们使用以下数据:
我正在使用set()来写入数据.现在,我希望用户编辑它们bookTitle,并且需要在两个地方进行更改.我尝试使用update()但似乎无法使其工作.我只能bookTitle在bookInfoNOT中编辑books.
移动不是一种选择,因为它会擦除bookData.我也尝试使用push()编写,但后来,我无法正常搜索,因为我没有pushID(我需要搜索,因为用户不能拥有两本同名书籍)
那么,有没有办法更新键值?或者,有更好的方法吗?我接受建议.谢谢!
更新:这是我目前用来更新里面的书名bookInfo
var bookName = document.getElementById('bookName').value;
firebase.database().ref('books/' + bookName + '/bookInfo').update({
bookTitle : bookName
});
Run Code Online (Sandbox Code Playgroud) 我一直在使用ForEach来填充我的HTML表格.
到目前为止这么好但桌子不是实时的.我必须重新加载它的功能才能再次获取结果.如果我添加或删除条目,则在重新加载之前不会发生任何错误.
有没有办法让这个实时?来自Firebase文档的代码:
var query = firebase.database().ref("users").orderByKey();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
// key will be "ada" the first time and "alan" the second time
var key = childSnapshot.key;
// childData will be the actual contents of the child
var childData = childSnapshot.val();
});
});
Run Code Online (Sandbox Code Playgroud)
请原谅我对JS的不了解,我正在研究它.