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

Firebase数据读取异常:console.log显示正确值但返回结果为undefined(React Native体重追踪器开发场景)

解决Firebase异步数据读取返回undefined的问题

嘿,我来帮你搞定这个问题!你遇到的核心问题是异步函数的返回值处理不当——虽然console.log能拿到正确数据,但你的readWeight函数并没有把异步请求的结果返回给调用它的地方,所以才会得到undefined。

第一步:修复readWeight函数,让它返回Promise

原来的函数里,get()是异步操作,你在.then()里返回了值,但整个readWeight函数本身没有返回任何内容。我们需要让它返回get()的Promise,这样外部就能通过async/await或者.then()获取结果:

const readWeight = (database, userId) => {
  // 返回get()的Promise,让外部可以等待异步操作完成
  return get(ref(database, `users/${userId}/weight`)).then((snapshot) => {
    try {
      if (snapshot.exists()) {
        console.log(`Weight found. Current weight is: ${snapshot.val()} kg`);
        return snapshot.val();
      } else {
        console.log("Weight wasn't found");
        return null; // 没有数据时返回null,避免后续显示undefined
      }
    } catch (error) {
      console.log(error);
      throw error; // 抛出错误,方便外部捕获处理
    }
  });
};

第二步:在HomeScreen中正确处理异步数据

React组件渲染时不能直接同步获取异步数据,我们需要用useState存储体重值,用useEffect触发异步请求:

// Modules
import { useState, useEffect } from "react";
import { Text, View } from "react-native";
import { LogOutButton } from "../Components/LogOutButton";
import { auth, database, readWeight } from "../firebase";
// Stylesheet
import { styles } from "../Stylesheet/Stylesheet";

export const HomeScreen = () => {
  const user = auth.currentUser;
  // 用状态存储体重,初始值设为null表示加载中
  const [currentUserWeight, setCurrentUserWeight] = useState(null);

  useEffect(() => {
    // 定义异步函数获取体重
    const fetchUserWeight = async () => {
      // 先判断用户是否已登录
      if (!user) return;
      
      try {
        const weight = await readWeight(database, user.uid);
        // 更新状态,触发组件重新渲染
        setCurrentUserWeight(weight);
      } catch (err) {
        console.error("获取体重失败:", err);
      }
    };

    // 调用异步函数
    fetchUserWeight();
  }, [user]); // 当user对象变化时,重新执行请求

  return (
    <View style={styles.profileMain}>
      <View style={styles.profileInfoContainer}>
        <View style={styles.HOME__profileWeightContainer}>
          <Text style={styles.HOME__profileWeightText}>
            {/* 处理加载状态和无数据的情况 */}
            Last Weight: {currentUserWeight !== null 
              ? `${currentUserWeight} kg` 
              : "Loading..."}
          </Text>
        </View>
      </View>
    </View>
  );
};

为什么之前的代码会返回undefined?

  • get()是异步操作,它不会立刻返回结果,而是在请求完成后通过.then()回调传递数据。
  • 你原来的readWeight函数没有返回任何值(return只在.then()的回调里),所以调用readWeight()时会立刻得到undefined,而console.log是等请求完成后才执行的,所以能拿到正确数值。

额外说明

  • 关于你提到的DataSnapshot:从Firebase的get()方法返回的结果确实是DataSnapshot类型,官方文档的内容是有效的,你可以放心参考。
  • 记得处理用户未登录的情况(比如user可能为null),避免调用readWeight时出现错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 15:43:12