Angular项目编译报错:FieldValue不存在arrayRemove属性
问题解决:Property 'arrayRemove' does not exist on type 'typeof FieldValue'
错误原因
- 导入不兼容:你使用的是
@angular/fire/compat/firestore(兼容版本),但却导入了原生Firebase的FieldValue,两者类型不匹配导致报错。 - 参数类型错误:
arrayRemove方法接受单个或多个元素作为参数,而非直接传入数组。
解决方案
1. 替换FieldValue的导入来源
删除从firebase/firestore导入的FieldValue,改为从@angular/fire/compat/firestore导入兼容版本:
import { Injectable } from '@angular/core'; // 从compat包同时导入AngularFirestore和FieldValue import { AngularFirestore, FieldValue } from '@angular/fire/compat/firestore'; import { Animal, Batuda } from '../interfaces/batuda.interface'; import { Usuari } from '../interfaces/user'; // 保留firebase核心导入(如果有其他使用场景) import firebase from 'firebase/app'; import 'firebase/firestore';
2. 修正arrayRemove的参数传递
使用扩展运算符...展开animal数组,将数组中的每个元素作为arrayRemove的参数:
borrarAnimal(path:string, id:string, animal:Animal[]){ const collection = this.angularFirestore.collection(path); return collection.doc(id).update({ 'animals': FieldValue.arrayRemove(...animal) }) }
3. 版本兼容性检查
确保@angular/fire与firebase版本匹配:
@angular/fire的compat分支对应Firebase v8.x版本,如果你的项目安装了Firebase v9+,要么降级到v8,要么切换到AngularFire的模块化API(非compat版本)。
内容的提问来源于stack exchange,提问作者Oriol Piqué
相关产品推荐
相关产品推荐

