【发布时间】:2021-09-12 02:14:20
【问题描述】:
我在此示例中使用 @angular/fire,但该问题对任何 firebase 连接都有效。
获取数据
this.afs.collection<Message>('messages' , ref =>
ref.where('chatId', '==', chatId)
)
.valueChanges()
.subscribe(msgs => {
this.removeUserFromUnseen(msgs)
});
将更改应用到结果中的同一集合
public removeUserFromUnseen(msgs: Message[]) {
let batch = this.afs.firestore.batch();
msgs.forEach(msg => {
const mesagesRef = this.afs.collection<Message>('messages').doc(msg.id).ref;
batch.update(mesagesRef , {notSeenBy : msg.notSeenBy.filter(userId => userId !== someId )});
});
return batch.commit();
}
当然,这会创建一个无限循环,因为我会监听“消息”集合的更改,然后将更改应用到它。 我不想.unsubscribe 使用.take 或任何类似的东西,所以我不认为这是一个重复的问题。
如何在不创建此循环的情况下执行此操作?
我可以只订阅特定属性的新条目或更改吗?我是否应该在可能的情况下重新考虑数据库的设计?
我正在使用firestore
【问题讨论】:
标签: typescript firebase google-cloud-firestore angularfire