Firebase更新数据无效 [英] Firebase update data not working
问题描述
您好我想更新firebase数据,但是当我更新它插入到一个新的数据,而不是更新我设置的具体数据,这是'hoo'的姓氏
Hi I wanted to update the firebase data, but when I update it insert to a new data instead of updating the specific data that I set which is the lastname equal to 'hoo'
fb.orderByChild("Lastname").equalTo('hoo').once("value", function(snapshot){
console.log(snapshot.ref());
console.log(snapshot.val());
// var snapref = snapshot.ref();
snapshot.ref().update({
FirstName: 'yoyo1'
});
})
编辑:
fb.orderByChild("Lastname").equalTo('hoo').once("child_added", function(snapshot){
console.log(snapshot.ref());
console.log(snapshot.val());
// var snapref = snapshot.ref();
snapshot.ref().update({
FirstName: 'yoyo1'
});
})
使用'child_added'当您针对Firebase数据库执行查询时,可能会有潜在的潜在威胁,而不是使用值 。
推荐答案
是多个结果。所以快照包含了这些结果的列表。即使只有一个结果,快照将包含一个结果列表。
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
这意味着当您调用 snapshot.ref( )
,你可以得到你所查询位置的引用( fb
)不是。
This means that when you call snapshot.ref()
, you get the reference to the location that you queried (fb
) not to the child that you selected.
解决方法是循环结果:
The solution is to loop over the results:
fb.orderByChild("Lastname").equalTo('hoo').once("child_added", function(snapshot){
snapshot.forEach(function(child) {
child.ref().update({
FirstName: 'yoyo1'
});
}
})
顺便说一下,我很确定我在过去几天给出了完全相同的答案,所以我强烈建议潜伏在 firebase-database 标签和学习,当你读到别人的奋斗。
By the way, I'm pretty sure I gave the exact same answer in the past few days. So I highly recommend lurking on the firebase-database tag and learning while you read what others struggle with.
这篇关于Firebase更新数据无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!