You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Angular中使用Firestore的arrayUnion:FieldValue报错排查

解决AngularFire中FieldValue不存在的问题

你的问题出在错误地从AngularFire的Firestore实例中访问FieldValue——FieldValue是Firebase JS SDK的核心工具类,并非AngularFire服务实例的属性,需要直接导入使用。

修正步骤:

  1. 从Firebase SDK中导入FieldValue:
    // 根据你的Firebase版本选择其中一种导入方式
    import { FieldValue } from '@firebase/firestore';
    // 或
    import { FieldValue } from 'firebase/firestore';
    
  2. 修改代码,将this.firestore.FieldValue替换为直接使用FieldValue:
const userRef = this.firestore.collection('users').doc(email);

userRef.update({
  lessonsWatched: FieldValue.arrayUnion(newLessonObject)
})

补充说明:

AngularFire只是Firebase的Angular封装层,它提供的是适配Angular的服务(如Firestore、AngularFireCollection等),但像FieldValue、Timestamp这类通用工具类,依然需要直接从Firebase核心SDK中导入,而非通过AngularFire的实例调用。

内容的提问来源于stack exchange,提问作者poshy23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 13:43:14