【问题标题】:In firestore "Property 'arrayUnion' does not exist on type 'typeof FieldValue'"在 firestore \"Property \'arrayUnion\' 不存在于类型 \'typeof FieldValue\'\"
【发布时间】:2023-01-23 01:51:23
【问题描述】:

我在我的角度项目中使用 @angular/fire": "7.4.0" 我只想对用户文档进行更新操作。在用户文档中有一个字段'groups',它是 firestore 中的数组类型。我想在该数组中插入一个新值。Check Image for user document structure

我使用以下语法进行更新操作-

//Add Group in user's groups array
  addGroupToUserDoc(gid: string, userId: string) {
    const userRef = this.db.doc(`users/${userId}`).update(
      {
        groups: FieldValue.arrayUnion(gid),
        
      }
    );
  }

错误-error received

这里 this.db 是“AngularFirestore”对象。我参考了以下两个资源并尝试了两种方法,但没有任何效果。

https://github.com/angular/angularfire/issues/2008

https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array

【问题讨论】:

    标签: angular firebase google-cloud-firestore angularfire


    【解决方案1】:

    您需要在 FieldValue 之前添加“firestore”。像这样:

    //Add Group in user's groups array
      addGroupToUserDoc(gid: string, userId: string) {
        const userRef = this.db.doc(`users/${userId}`).update(
          {
            groups: firestore.FieldValue.arrayUnion(gid),
            
          }
        );
      }
    

    并从 firestore/app 导入 firestore

    import { firestore } from 'firebase/app';
    

    【讨论】:

      【解决方案2】:

      就我而言,在 Node 环境中使用 SDK 我不得不使用

      import * as admin from 'firebase-admin'

      admin.firestore.FieldValue.arrayUnion(gid)

      【讨论】:

        猜你喜欢
        • 2019-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-09
        • 2016-03-30
        • 2018-11-17
        • 1970-01-01
        相关资源
        最近更新 更多