如何在AngularFirestore中使用arrayUnion? [英] How to use arrayUnion with AngularFirestore?

查看:41
本文介绍了如何在AngularFirestore中使用arrayUnion?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本数据库,该数据库实际上在用户下方存储了一个产品ID数组.用户可以选择要添加到阵列中的产品,因此使用'arrayUnion'是有意义的,因此避免了不断读取和重写阵列,但是,我不断收到错误*类型'上不存在属性'firestore'FirebaseNamespace'."

I have a basic database that essentially stores an array of product id's underneath a user. The user can select products to add to the array so it makes sense to use 'arrayUnion' so I avoid reading and re-writing the array constantly, however, I keep getting the error *"Property 'firestore' does not exist on type 'FirebaseNamespace'."

我已遵循以下文档中的文档: https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array ,但我担心我仍然使用不正确.

I've followed the documentation found at: https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array but I fear I'm still using it incorrectly.

我更新数组的代码是:

 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)
    });
  }

这篇关于如何在AngularFirestore中使用arrayUnion?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆