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

Firebase.getFloat返回错误值求助:写入成功但读取返回0.0

Firebase.getFloat() Returns 0.0 After Successfully Pushing Value with pushFloat()

Let's break down why you're seeing this issue and how to fix it quickly.

The Root Cause

Your code uses Firebase.pushFloat("/Num/number", 42.0)—but here's the key detail you might have missed: the push family of methods in the FirebaseArduino library doesn't set the value of the path you specify directly. Instead, it creates a new child node under that path with a unique auto-generated ID (like -NxYza1b2C3d) and stores your value there.

So your actual Firebase data structure looks like this:

Num
  number
    -NxYza1b2C3d: 42.0

When you call Firebase.getFloat("/Num/number"), you're trying to read an object (the number node containing the auto-ID child) as a float. Since the library can't parse an object into a float, it returns the default value of 0.0.

Fix 1: Use setFloat Instead of pushFloat

If your goal is to directly set the value of /Num/number to 42.0, replace pushFloat with setFloat:

// Replace pushFloat with setFloat to assign the value directly to the path
Firebase.setFloat("/Num/number", 42.0);

This will store the float value directly at /Num/number, and your subsequent Firebase.getFloat("/Num/number") call will return 42.0 as expected.

Fix 2: If You Need to Use push (For Multiple Entries)

If you intended to use push to add multiple entries with unique IDs (common for list-like data), you can capture the auto-generated path returned by pushFloat and use that to read the value:

// Capture the exact path of the newly pushed node
String pushedPath = Firebase.pushFloat("/Num/number", 42.0);

if (!Firebase.failed()) {
  // Read the value using the captured path
  Serial.print("Pushed value: ");
  Serial.println(Firebase.getFloat(pushedPath));
}

Additional Troubleshooting Tips

  • Check your Firebase Console: Navigate to your project's Realtime Database view to see the actual structure of your data. This will immediately confirm if the value is stored under an auto-ID child instead of the path you expected.
  • Inspect raw data: Use Firebase.get("/Num/number") to print the raw data at that path. If it returns a JSON object (like {"-NxYza1b2C3d":42.0}), that confirms the push behavior is the issue.
  • Verify permissions: Ensure your database rules allow read access to the path (though you could push successfully, it's worth double-checking if reads are restricted).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 09:37:27