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

React Native+Firestore报错:t._freezeSettings不是函数(未处理Promise拒绝)

解决React Native中Firestore使用时的常见错误

可能的错误原因及修复方案

1. Auth状态未就绪导致currentUser为null

直接调用firebase.auth().currentUser.email时,React Native中auth状态初始化需要时间,组件挂载阶段currentUser可能还未加载完成,会导致无法获取邮箱甚至抛出错误。

修复方式:监听auth状态变化,确保获取到有效用户后再执行Firestore操作:

import { onAuthStateChanged } from "firebase/auth";
import { doc, setDoc, query, where } from "firebase/firestore";

// 在组件内使用useEffect监听auth状态
useEffect(() => {
  const unsubscribe = onAuthStateChanged(firebase.auth(), (user) => {
    if (user) {
      const email = user.email;
      const dailyRef = firebase.firestore().collection('daily');

      // 写入文档
      setDoc(doc(dailyRef, email), {
        name: "name",
        email: email
      });

      // 创建查询
      const q = query(dailyRef, where("email", "==", email));
    } else {
      // 处理用户未登录的情况
    }
  });

  // 组件卸载时取消监听
  return () => unsubscribe();
}, []);

2. Firebase模块化API导入错误

若使用Firebase v9及以上版本,必须正确导入所需的Firestore函数,否则会因函数未定义报错。

确保导入语句正确:

// 导入Firebase Auth和Firestore的必要函数
import { getAuth, onAuthStateChanged } from "firebase/auth";
import { doc, setDoc, query, where, getFirestore, collection } from "firebase/firestore";

// 使用模块化方式获取实例
const auth = getAuth();
const db = getFirestore();
const dailyRef = collection(db, 'daily');

3. 文档ID空值或非法字符问题

用邮箱作为文档ID时,需确保邮箱不为空,同时Firestore文档ID禁止包含/(邮箱中的@和.是合法的)。添加空值检查避免报错:

if (user?.email) {
  const email = user.email;
  // 执行Firestore操作
}

内容的提问来源于stack exchange,提问作者Ulaş Kara

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 13:25:33