我的消息 child 有 child 的属性已读。我想将其设置为 true。我如何在 JavaScript 中做到这一点? (我想在单个命令中读取设置为 true 的所有子消息的属性)。
我试过let ref = yield firebase.database().ref(groups/${groupId}/messages).update({read:true})
但没有用。
我也想知道这是否是一个好的数据库结构
谢谢:)
最佳答案
您首先必须读取 messages
节点以获取其所有子节点(使用 once()
方法),然后使用 update()
一次调用更新所有子节点的方法。
下面的代码应该可以解决问题:
//set groupId value
var db = firebase.database();
db.ref(`groups/${groupId}/messages`)
.once('value')
.then(function (snapshot) {
var updates = {};
snapshot.forEach(function (childSnapshot) {
var key = childSnapshot.key;
updates[`groups/${groupId}/messages/${key}/read`] = true;
});
return db.ref().update(updates);
});
另请参阅https://firebase.google.com/docs/database/web/read-and-write#update_specific_fields
<小时/>“我也想知道这对于数据库来说是否是一个好的结构” -> 这是一个相当广泛的问题,其答案取决于许多因素,包括您的用户要求、应用程序的屏幕等...
关于javascript - 如何在 firebase 数据库中的所有消息子级中将 "read"更新为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904052/