我尝试更新
firestore 中的字段,但找不到正确的调用:
changeAlertState(senderId, receiverId, alertType, bool){
let type = alertType == 'toBe' ? 'toBeAlerted' : 'toAlert';
this.afs.firestore
.collection("books")
.doc(senderId + '/' + receiverId)
.update({
[type]: bool
})
.then(() => {
console.log("Contact " + receiverId + " alert successfully updated!");
});
}
我收到此错误:
FirebaseError: [code=invalid-argument]: Invalid document reference. Document references must have an even number of segments, but books/33KlbrBypXMe888vpO7dXgDVrfY2/hLh7Ao7IABZBukEpGFK1I8lq1rx1 has 3
最佳答案
您正在将部分字段路径传递到对 doc()
的调用中。这是行不通的,因为您需要(仅)将文档 ID 传递到 doc
中。之后,您可以通过使用 分隔路径的各个部分,为要更新的字段构建字段路径。
var value = {};
value[receiverId+"."+type] = bool;
this.afs.firestore
.collection("books")
.doc(senderId)
.update(value)
关于javascript - 如何更新firestore子集合字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54130061/