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

Expo Firebase中使用arrayUnion更新Firestore文档生产环境报错

问题:Firestore生产环境调用arrayUnion()时抛出undefined字段错误

问题场景

我编写了向Firestore的companies文档添加评论数据的代码,开发环境运行正常,但生产环境抛出错误。

核心代码

try {
  const id = Date.now();
  const obj = {
    id:id,
    user:user,
    company:company,
    role:role,
    transmission:transmission,
    miles:miles,
    oldCar:oldCar,
    payMile:payMile,
    treat:treat,
    homeTime:homeTime,
    orientation:orientation,
    text:text,
    score: Math.round(((treat * 5 + homeTime * 5 + orientation * 5 + overvall * 5 + mintenance * 5 + dispatchers * 5) / (5 + 5 + 5 + 5 + 5 + 5)) *10,) / 10,
    likes: [],
    job:job,
    firstCon:firstCon,
    gross:gross,
    equipment:equipment,
    drvPosition:drvPosition,
    overvall:overvall,
    mintenance:mintenance,
    dispatchers:dispatchers,
  }
  if (obj) {
    await updateDoc(doc(db, "companies", company.key), {
      "Reviews": arrayUnion(obj),
      "maxReviews": company?.maxReviews ? parseInt(company?.maxReviews) + 1 : 0,
    })
    console.log("review sended");
  } else {
    throw new Error("review object is not defined");
  }
} catch (error) {
  throw new Error("Error send review:" + error?.message);
}

生产环境错误信息

Error send review:Function arrayUnion() called with invalid data. Unsupported field value: undefined (found in document companies/zhVuyv94Y4FrRBbA0DdN)

已尝试操作

  • 将Firebase升级至最新版本(当前firebase版本为^10.5.0)
  • 多次修改代码但问题依旧

项目依赖配置(package.json)

{
  "name": "track",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "expo start --dev-client",
    "android": "expo run:android",
    "ios": "expo run:ios",
    "web": "expo start --web"
  },
  "dependencies": {
    "@expo/vector-icons": "^13.0.0",
    "@expo/webpack-config": "^0.17.0",
    "@react-native-async-storage/async-storage": "~1.17.3",
    "@react-native-community/netinfo": "9.3.5",
    "@react-navigation/drawer": "^6.5.0",
    "@react-navigation/native": "^6.0.13",
    "@react-navigation/stack": "^6.3.11",
    "axios": "^1.2.2",
    "expo": "~47.0.14",
    "expo-auth-session": "~3.8.0",
    "expo-blur": "~12.0.1",
    "expo-build-properties": "~0.4.1",
    "expo-crypto": "~12.0.0",
    "expo-dev-client": "~2.0.1",
    "expo-device": "~5.0.0",
    "expo-image-manipulator": "~11.0.0",
    "expo-image-picker": "~14.0.2",
    "expo-linear-gradient": "~12.0.1",
    "expo-mail-composer": "~12.0.0",
    "expo-notifications": "~0.17.0",
    "expo-random": "~13.0.0",
    "expo-sharing": "~11.0.1",
    "expo-splash-screen": "~0.17.5",
    "expo-status-bar": "~1.4.0",
    "firebase": "^10.5.0",
    "lodash": "^4.17.21",
    "moment": "^2.29.4",
    "patch-package": "^6.5.1",
    "prettier": "^3.0.3",
    "prop-types": "^15.8.1",
    "react": "18.1.0",
    "react-dom": "18.1.0",
    "react-native": "0.70.8",
    "react-native-dialog": "^9.3.0",
    "react-native-gesture-handler": "~2.8.0",
    "react-native-inset-shadow": "^1.0.3",
    "react-native-reanimated": "~2.12.0",
    "react-native-safe-area-context": "4.4.1",
    "react-native-screens": "~3.18.0",
    "react-native-switch-toggle": "^2.2.1",
    "react-native-web": "~0.18.7",
    "react-redux": "^8.0.5",
    "redux": "^4.2.0",
    "redux-thunk": "^2.4.2",
    "styled-components": "^5.3.6",
    "typesense": "^1.5.3"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9"
  },
  "private": true,
  "author": "",
  "license": "ISC",
  "description": ""
}

其他疑问

  • 不清楚错误具体触发位置,也不知道如何在iOS中记录日志
  • 奇怪的是,我自己的手机上生产环境运行正常,错误是在执行obj存在性判断后的代码时抛出的

解决办法

  1. 排查obj中的undefined字段
    错误明确指向arrayUnion()传入的数据包含undefined字段,你判断obj存在没用,obj内部的某个属性可能是空值。开发环境测试时数据全量填充不会出现问题,但生产环境用户可能漏填字段。
    可以添加校验代码:

    // 检查obj所有属性是否存在undefined
    const hasUndefined = Object.values(obj).some(val => val === undefined);
    if (hasUndefined) {
      throw new Error("评论数据存在未填充字段");
    }
    

    或者给可能缺失的字段设置默认值:

    const obj = {
      id: id,
      user: user || '',
      company: company || {},
      role: role || '',
      transmission: transmission || '',
      // 其他字段按业务逻辑补全默认值
      score: Math.round(((treat * 5 + homeTime * 5 + orientation * 5 + overvall * 5 + mintenance * 5 + dispatchers * 5) / 30) *10) / 10,
      likes: [],
    }
    
  2. iOS日志记录方法

    • 用console.log输出关键信息,将设备连接到Xcode,在Console面板查看日志
    • 集成Sentry等第三方日志工具,将错误日志上传至服务端查看
    • 利用AsyncStorage将日志写入本地存储,后续导出查看
  3. 额外优化点

    • 计算score的分母直接写30即可,无需重复相加6个5,避免出错
    • 先校验company.key是否存在,防止文档路径错误
    • 将parseInt(company?.maxReviews)改为parseInt(company?.maxReviews) || 0,避免出现NaN值

内容的提问来源于stack exchange,提问作者Салих Гаянов

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 21:15:00