通过orderByChild进行firebase抓取,然后更新结果键 [英] firebase grab by orderByChild then update results key
问题描述
所以我有此查询,当前收集的所有数据的 materialName
等于黄金.我想将所有内容更改为false.
//materialName ="gold"例如database.ref('/app/posts').orderByChild('material').startAt(materialName).endAt(materialName).once('value',function(snapshot){const materials = snapshot.val();})
我尝试过这样的事情:
database.ref('/app/posts').orderByChild('material').startAt(materialName).endAt(materialName).once('value',function(snapshot){database.ref('/app/posts').update({material:false});})
我也尝试过这个:
const newData = Object.assign({},materials,{material:false});//,但此更新在帖子之外,结果将是:帖子":{材料":假,"post-1503586":{"title":样本标题",材料":金"},"post-9172991":{"title":样本标题",材料":银"}}
样本json:
帖子":{"post-1503586":{"title":样本标题",材料":金"},"post-9172991":{"title":样本标题",材料":银"}}
您需要遍历结果(因为可以有多个匹配的节点),然后更新每个结点:
database.ref('/app/posts').orderByChild('material').equalTo(materialName).once('value',function(snapshot){snapshot.forEach(function(child){child.ref.update({material:false});});});
您还将注意到,我将您的 So i have this query and currently collects all the data with I have tried something like this: also I have tried this: sample json:
You need to loop over the results (since there can be multiple matching nodes) and then update each: You'll also note that I changed your 这篇关于通过orderByChild进行firebase抓取,然后更新结果键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! .startAt().endAt()
更改为materialName
equals to gold. I wanted change all to false.// materialName = "gold" for example
database.ref('/app/posts').orderByChild('material').startAt(materialName).endAt(materialName).once('value', function (snapshot) {
const materials = snapshot.val();
})
database.ref('/app/posts').orderByChild('material').startAt(materialName).endAt(materialName).once('value', function (snapshot) {
database.ref('/app/posts').update({material: false});
})
const newData = Object.assign({}, materials, {material: false});
// but this updates outside of the post, result will be:
"posts" : {
"material": false,
"post-1503586" : {
"title": "sample title",
"material" : "gold"
},
"post-9172991" : {
"title": "sample title",
"material" : "silver"
}
}
"posts" : {
"post-1503586" : {
"title": "sample title",
"material" : "gold"
},
"post-9172991" : {
"title": "sample title",
"material" : "silver"
}
}
database.ref('/app/posts')
.orderByChild('material')
.equalTo(materialName)
.once('value', function (snapshot) {
snapshot.forEach(function(child) {
child.ref.update({material: false});
});
});
.startAt().endAt()
to an equalTo()
, which gives the same results with less code.