在 Cloud-Firestore 中读取文档的未知响应
Unknown response for document read in Cloud-Firestore
最近在玩firebase,想实现一个自增自减机制。当前值应存储在 Cloud Firestore 中。
所以我引用了它并检查了 id 以及引用是否存在。
当我尝试 运行 代码时,我得到 正确的 ID 并且 payload.exist 是 true .
如果我现在想添加检查数字是否为 0 以将其删除,文档快照是 未知,所以我找不到 属性或在文档上调用 delete/update。
如果有人能告诉我如何访问文档中的数据,那就太好了。
谢谢! :)
在这里你可以找到附加的代码:
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 });
}
});
}
如 doc 中所述,snapshotChanges()
returns 数据的 Observable 作为 DocumentChangeAction
。
而对于 DocumentChangeAction
type:
A DocumentChangeAction
gives you the type and payload properties. ... The payload property is a DocumentChange
which
provides you important metadata about the change and a doc
property
which is the DocumentSnapshot
.
所以下面应该可以解决问题(未经测试):
const quantity = (item.payload.doc.data().currentNumber ...
我可以通过调用 .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
}
感谢大家的帮助! :)
最近在玩firebase,想实现一个自增自减机制。当前值应存储在 Cloud Firestore 中。
所以我引用了它并检查了 id 以及引用是否存在。
当我尝试 运行 代码时,我得到 正确的 ID 并且 payload.exist 是 true .
如果我现在想添加检查数字是否为 0 以将其删除,文档快照是 未知,所以我找不到 属性或在文档上调用 delete/update。
如果有人能告诉我如何访问文档中的数据,那就太好了。 谢谢! :)
在这里你可以找到附加的代码:
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 });
}
});
}
如 doc 中所述,snapshotChanges()
returns 数据的 Observable 作为 DocumentChangeAction
。
而对于 DocumentChangeAction
type:
A
DocumentChangeAction
gives you the type and payload properties. ... The payload property is aDocumentChange
which provides you important metadata about the change and adoc
property which is theDocumentSnapshot
.
所以下面应该可以解决问题(未经测试):
const quantity = (item.payload.doc.data().currentNumber ...
我可以通过调用 .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
}
感谢大家的帮助! :)