Joh*_*erp 1 javascript firebase firebase-realtime-database
所以我有这个查询,目前收集materialName等于黄金的所有数据。我想把所有的都改成假。
// materialName = "gold" for example
database.ref('/app/posts').orderByChild('material').startAt(materialName).endAt(materialName).once('value', function (snapshot) {
const materials = snapshot.val();
})
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情:
database.ref('/app/posts').orderByChild('material').startAt(materialName).endAt(materialName).once('value', function (snapshot) {
database.ref('/app/posts').update({material: false});
})
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
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"
}
}
Run Code Online (Sandbox Code Playgroud)
示例json:
"posts" : {
"post-1503586" : {
"title": "sample title",
"material" : "gold"
},
"post-9172991" : {
"title": "sample title",
"material" : "silver"
}
}
Run Code Online (Sandbox Code Playgroud)
您需要遍历结果(因为可以有多个匹配的节点),然后更新每个:
database.ref('/app/posts')
.orderByChild('material')
.equalTo(materialName)
.once('value', function (snapshot) {
snapshot.forEach(function(child) {
child.ref.update({material: false});
});
});
Run Code Online (Sandbox Code Playgroud)
您还会注意到我将您的更改.startAt().endAt()为 an equalTo(),这会以更少的代码提供相同的结果。
| 归档时间: |
|
| 查看次数: |
887 次 |
| 最近记录: |