首页 > 解决方案 > Angularfire:valuechanges observable 发出两次

问题描述

语境 :

我正在与 Angularfire 6.1.5 和 firebase 8.6.1 聊天

我的 Firestore 包含一个集合“讨论”。每个讨论都包含一个子集合“消息”。

我的问题是关于新消息的处理程序部分。我需要接收某人创建的最后一条新消息并显示它。

代码 :

我正在用这个 observable 处理新消息:

return this.db.collection<Message>(`discussions/${id}/messages`, ref => ref.orderBy('createdAt').limitToLast(1))
      .valueChanges({ idField: '_id' })
      .pipe(...);

添加消息的方法:

sendMessage(discussionId: Discussion['_id'], message: Message): void {
    this.db.collection<Message>(`discussions/${discussionId}/messages`).add(message);
  }

问题 :

当我只添加一条消息时,valueChanges()会发出两次。我只有一个订阅者。

这种双重发射是设计使然还是错误?

谢谢 :)

标签: angularfirebaserxjsangularfire2

解决方案


推荐阅读