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

React Native中从Firestore获取当前登录用户信息报错排查

问题解决:Firestore获取当前用户信息报错

错误原因分析

你遇到的错误翻译为:

Error: firebase.firestore().collection().get(*) 'options'必须是一个对象,js引擎: hermes

问题出在get(firebase.auth().currentUser.uid)这一行——Firestore的collection.get()方法只接收查询配置对象,不能直接传文档ID。要获取单个用户的文档,得先通过doc(文档ID)定位到具体文档,再调用get()。

修正后的代码

const currentUser = firebase.auth().currentUser;
if (currentUser) {
  firestore()
    .collection('users')
    .doc(currentUser.uid) // 先通过doc()指定用户对应的文档
    .get()
    .then(docSnapshot => {
      if (docSnapshot.exists) {
        const userData = docSnapshot.data();
        console.log('用户名:', userData.username, '邮箱:', userData.email);
      } else {
        console.log('未找到该用户的文档');
      }
    })
    .catch(error => {
      console.error('获取用户数据失败:', error);
    });
} else {
  console.log('当前没有用户登录');
}

关键说明

  • 先判断currentUser是否存在,避免用户未登录时出现uid为undefined的错误
  • 单个文档的快照是docSnapshot,不需要用forEach遍历,直接通过exists判断文档是否存在,再用data()取出用户数据

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 21:30:47