javascript - 如何更新firestore子集合字段

标签 javascript firebase google-cloud-firestore

我尝试更新 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!");
            });
}

这是数据库: enter image description here

我收到此错误:

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)

另请参阅documentation on updating fields in a nested object .

关于javascript - 如何更新firestore子集合字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54130061/

相关文章:

javascript - 具有右对齐第二列的 CSS Flex 换行到左侧而不是在小窗口之间 float

javascript - JavaScript 中的多维关联数组

firebase - flutter firebase 用户在应用程序启动时登录

android - 如何确保不是每个人都可以使用ItemTouchHelper?

android - 当使用发布 apk 时,某些代码已被 Dart AOT 编译器 (TFA) 删除

javascript - 有没有办法在 html5 Canvas 上显示 html 元素 "progress"?

javascript - Jquery datepicker 输入普通文本和日期

android - 如何显示 Firebase 存储或从 Firebase 存储获取图像

firebase - 在数据库服务器中管理 FCM 注册 ID 的最佳方法是什么?

angular - 在组件中显示服务变量