如何在 AngularFirestore 中使用 arrayUnion?

Pet*_*995 5 javascript firebase google-cloud-firestore

我有一个基本的数据库,它本质上在用户下面存储了一系列产品 ID。用户可以选择要添加到数组中的产品,因此使用“arrayUnion”是有意义的,因此我避免不断读取和重写数组,但是,我不断收到错误 *“属性 'firestore' 不存在于类型 ' 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)
    });
  }
Run Code Online (Sandbox Code Playgroud)

Pet*_*dad 6

首先你需要导入firestore

import { firestore } from 'firebase/app';
Run Code Online (Sandbox Code Playgroud)

然后你将能够使用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)
    });
  }
Run Code Online (Sandbox Code Playgroud)


dan*_*y74 6

import { arrayUnion } from '@angular/fire/firestore'

const path = `ai/${videoId}/panel-operation/${id}`
const myDoc: AngularFirestoreDocument<any> = this.afs.doc<any>(path)

const promise: Promise<void> = myDoc.update({ auxPanelSelections: arrayUnion({auxPanel: 'clip', operation: 'replace'}) }).catch((err: any) => {
  console.error(`oopsie - ${err.message}`)
  return null
})
Run Code Online (Sandbox Code Playgroud)

auxPanelSelectionsmyDoc是文档中的一个数组

请注意,上面的代码也可以完美地工作arrayRemove

我找不到@angular/fire相关文档arrayUnion,但通用文档在这里