javascript - 如何将 arrayUnion 与 AngularFirestore 一起使用?

标签 javascript firebase google-cloud-firestore

我有一个基本数据库,它本质上存储了用户下方的产品 ID 数组。用户可以选择要添加到数组的产品,因此使用 'arrayUnion' 是有意义的,因此我避免不断读取和重写数组,但是,我不断收到错误 *"Property 'firestore' does not exist on type ' Firebase 命名空间'。”

我遵循了以下位置的文档:https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array但我担心我仍然在错误地使用它。

我更新数组的代码是:

 addToPad(notepadName: string){
    const updateRef = this.db.collection('users').doc(this.activeUserID).collection('notepads').doc(notepadName);
    updateRef.update({
      products: firebase.firestore.FieldValue.arrayUnion(this.productId)
    });
  }

最佳答案

首先需要导入firestore :

import { firestore } from 'firebase/app';

然后你就可以使用 arrayUnion :
addToPad(notepadName: string){
    const updateRef = this.db.collection('users').doc(this.activeUserID).collection('notepads').doc(notepadName);
    updateRef.update({
      products: firestore.FieldValue.arrayUnion(this.productId)
    });
  }

关于javascript - 如何将 arrayUnion 与 AngularFirestore 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59429562/

相关文章:

javascript - 调用方法在 JavaScript 中不起作用

android - 带有错误数据的 Firebase 查询不会给出任何回调

database - Flutter:如何为我们要上传到Firebase数据库的每个元素设置数据类型?

android - 预防多个 Android 设备上的 Firebase 同一帐户

ios - 如何计算切片中的细胞总数?

android - 批量更新会触发多少次 onUpdate 函数?

javascript - 为什么这个 for() 循环不起作用?

javascript - onclick div 添加类

javascript - jquery,未选中时删除表行

javascript - Web Firestore - 将 Double 值保存到 Firebase