【发布时间】:2019-11-28 16:46:48
【问题描述】:
我正在尝试找出一种更优雅的方式来做到这一点......
我正在存储消息对象并希望获取包含新消息的对话总数。我的节点结构是这样的:
/messages/{userId}/{receipientId}/{messageId}/{message: '', senderId: '', status: 'new|read' }
*** messageId 是从 firebase 自动生成的密钥。 userId 和receipientId 是来自auth 的ID。
我为获得徽章中的数字而编写的第一个代码是这样的:
firebase.database().ref()
.child(`/messages/${userId}/${receipientId}`)
.orderByChild('status')
.equalTo('new')
.once('value')
.then(innerSnapshot => {
if (innerSnapshot.val() !== null) {
if (newConversations.findIndex(item => item === entry) === -1) {
newConversations.push(entry);
}
}
})
.catch();
这需要我将其包装在另一个查询中,以在为找到的每个项目运行单独的查询之前获取所有接收者 ID。它可以工作...但是非常暴力。
基本上,我只需要知道在其下方某处具有“status: 'new'”条目的receipientId 节点的数量。
【问题讨论】:
标签: reactjs typescript firebase firebase-realtime-database