【发布时间】: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