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

Firebase Firestore读取文档触发“undefined is not a function”错误

问题描述

尝试用以下代码从Firebase Firestore读取文档:

const getGameData = async () => {
  const docRef = doc(db, "games", gamePin);

  try {
    const docSnap = await getDoc(docRef);
    if (docSnap.exists()) {
      console.log("Document data:", docSnap.data());
      return docSnap.data();
    } else {
      console.log("No such document!");
      return null;
    }
  } catch (error) {
    console.error("Error getting document:", error);
  }
};

但始终在node_modules/@firebase/firestore/dist/index.rn.js第1046行触发**"undefined is not a function"**错误。奇怪的是,写入数据的代码完全正常:

const createGame = async () => {
  const pin = await getNewPin();
  const gameRef = doc(db, "games", pin.toString());
  const players = [];
  players.push({ userId, name });
  await setDoc(gameRef, {
    pin,
    gameType,
    streamingServices,
    creator: userId,
    players: players,
    gameState,
  });
  setGamePin(pin);
};

添加日志后发现代码无法执行过const docRef = doc(db, "games", gamePin);这一行,终端无日志输出。尝试将Firebase版本从10.7.2降级到10.5.2,问题依旧。

排查与解决方案

1. 校验gamePin的类型与有效值

写入时用pin.toString()作为文档ID,但读取时直接传入gamePin。如果gamePin不是字符串类型(比如数字、undefined或null),会触发doc()内部调用异常:

  • 先打印gamePin的类型和值确认:
    console.log(typeof gamePin, gamePin);
    
  • 读取时强制转为字符串:
    const docRef = doc(db, "games", gamePin.toString());
    

2. 统一Firestore方法的导入路径

确保所有Firestore相关方法都从同一个包导入,避免混用不同版本的导入源:

import { doc, getDoc, setDoc } from "firebase/firestore";
import { db } from "./your-firebase-config-file"; // 确认db初始化逻辑正确

3. 确认db实例的有效性

在getGameData开头打印db,验证它是已初始化的Firestore实例,而非undefined或null:

console.log(db);

如果是在组件中使用,确保db通过全局变量或上下文正确传递,没有在初始化完成前调用getGameData。

4. 捕获doc()调用的完整错误信息

doc()是同步方法,单独捕获它的错误并打印完整调用栈,能定位具体是哪个内部函数未定义:

const getGameData = async () => {
  try {
    console.log("Before doc call:", gamePin, db);
    const docRef = doc(db, "games", gamePin.toString());
    console.log("Doc ref created:", docRef);
    const docSnap = await getDoc(docRef);
    if (docSnap.exists()) {
      console.log("Document data:", docSnap.data());
      return docSnap.data();
    } else {
      console.log("No such document!");
      return null;
    }
  } catch (error) {
    console.error("Error in getGameData:", error);
    console.error("Error stack:", error.stack);
  }
};

5. 清理依赖缓存并重新安装

依赖缓存异常可能导致代码打包错误:

  • 删除依赖目录和锁文件:
    rm -rf node_modules package-lock.json yarn.lock
    
  • 重新安装依赖:
    npm install # 或 yarn install
    
  • 清除React Native缓存:
    npx react-native start --reset-cache
    

6. 检查Firebase初始化逻辑

确保Firebase初始化在调用getGameData前完成,且为同步初始化:

// firebase-config.js
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = { /* 你的配置信息 */ };

const app = initializeApp(firebaseConfig);
const db = getFirestore(app); // 同步初始化,保证db随时可用

export { db };

避免在异步函数中初始化db,导致调用读取方法时实例未就绪。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 12:30:00