【问题标题】:Unknown response for document read in Cloud-Firestore在 Cloud-Firestore 中读取文档的未知响应
【发布时间】:2020-09-30 12:25:28
【问题描述】:

目前,我玩了一下firebase,想实现一个递增和递减机制。当前值应存储在 Cloud Firestore 中。

所以我引用了它并检查了 id 以及引用是否存在。

当我尝试运行代码时,我得到了 正确的 id 并且 payload.exist 为 true

如果我现在想添加一个检查数字是否为 0 来删除它,文档快照是 unknown,所以我找不到该文档的属性或调用删除/更新。

如果有人能告诉我如何访问 Document 中的数据,那就太好了。 谢谢! :)

这里可以找到附件代码:

constructor(private db: AngularFirestore) { }
private updateNumberCount(count: number) {
const reference = this.db.collection('counting').doc('documentId');

reference.snapshotChanges().pipe(take(1)).subscribe(item => {
  console.log(item.payload.id);     // the correct id is displayed
  console.log(item.payload.exists); // -> is true

  const quantity = (item.currentNumber || 0) + count;
  if (quantity === 0) {
    // if it is 0 it should be deleted
    item.delete();
  } else {
    // if it not exists it should be created or updated
    item.update({ currentNumber });
  }
});
}

【问题讨论】:

  • 您好,请将您的代码添加为formatted text 并且不是图片。使用文本,它允许其他用户复制/粘贴以写下他们的答案。
  • 哦,对了。我附上了! :)
  • 我建议完全删除图片,只显示相关代码的所有文本。

标签: angular firebase google-cloud-firestore rxjs angularfire2


【解决方案1】:

正如doc 中所解释的,snapshotChanges() 将数据的 Observable 作为 DocumentChangeAction 返回。

对于DocumentChangeAction type

DocumentChangeAction 为您提供类型和有效负载属性。 ...有效负载属性是DocumentChange 为您提供有关更改的重要元数据和 doc 属性 这是DocumentSnapshot

所以以下应该可以解决问题(未经测试):

const quantity = (item.payload.doc.data().currentNumber  ...

【讨论】:

  • 感谢 Renaud 的快速回复!我尝试使用 payload.data() 接收数据并查看了文档(这对我有很大帮助!)不幸的是我无法解决我的问题。它仍然告诉我它无法解析未知类型的属性。我也找不到如何在文档中让它知道...
  • 如果我没记错的话是payload.doc.data(),而不是payload.data()
  • 文档也找不到。我发现了一种解决问题的不同方法。谢谢你的帮忙!! :)
【解决方案2】:

我可以通过对文档的引用调用 .ref.get().then() 来取回数据。

const reference = this.db.collection('counting').doc('documentId');

reference.ref.get().then(doc => {
// here I can call **.data** on it to receive the data. 
   doc.data().currentNumber 
}

感谢大家的帮助! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-12
    • 2018-11-19
    • 1970-01-01
    • 2021-09-27
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多