Firebase模拟器云函数报错:TypeError: Cannot read properties of undefined (reading 'arrayRemove')
解决Firebase云函数arrayRemove报错问题
问题原因
你遇到的TypeError: Cannot read properties of undefined (reading 'arrayRemove')报错,本质是代码中无法正确获取FieldValue对象,导致调用arrayRemove方法时失败,常见原因是Firebase Admin SDK的版本兼容问题或FieldValue的引用方式不正确。
解决方法
方法1:修正FieldValue的引用方式
直接通过admin.firestore.FieldValue调用方法,避免中间变量赋值可能引发的异常:
transaction.update(committeeRef, { "countries": admin.firestore.FieldValue.arrayRemove({ "name": chosenCountry, "taken": false, }) });
方法2:单独导入FieldValue
如果你的Firebase Admin SDK版本是v10及以上,推荐直接从firestore模块导入FieldValue,更贴合模块化规范:
// 在文件顶部添加导入语句 const functions = require("firebase-functions"); const admin = require("firebase-admin"); const { FieldValue } = require("firebase-admin/firestore"); admin.initializeApp(); // 后续代码中直接使用FieldValue transaction.update(committeeRef, { "countries": FieldValue.arrayRemove({ "name": chosenCountry, "taken": false, }) });
方法3:更新Firebase Admin SDK版本
若你的SDK版本过旧,可能存在FieldValue属性路径的变更问题,执行以下命令更新到最新稳定版:
npm install firebase-admin@latest --save
额外检查
确保committeeRef是有效的Firestore文档引用,避免因引用未正确初始化导致的连锁错误。
内容的提问来源于stack exchange,提问作者Electric Dragon
相关产品推荐
相关产品推荐

