将Firebase子项命名为序列数组 [英] Name Firebase child as sequence array

查看:59
本文介绍了将Firebase子项命名为序列数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Fusionchart读取我的Firebase数据并在Web应用程序中创建一个图表.但是我的Firebase数据库结构错误,因此Fusionchart无法获取数据(我的Firebase配置正确).

I want to use Fusionchart to read my Firebase data and create a chart in my web app. but my Firebase DB has a wrong structure, so the Fusionchart can't get data (my Firebase config is right).

以下是我将数据写入Firebase的代码,num是每个循环中增加的值.但是,如附图所示,孩子的名字没有添加为序列号. 另一个问题是,我不希望孩子1内有唯一键,而孩子内部只有6种键就可以了.

Following is the code that I write data to Firebase, num is a value increased in each loop. But as shown in the attached picture, the child's name is not added as a sequence number. Another question is I don't want the unique key inside the child 1, just six various inside the child one is ok.

任何建议将不胜感激.

Any suggestions will be appreciated.

firebase.database().ref('testdata/User1').child(num).push({

 x: posX,

  y: posY,

MaxSpeed: maxSpeed, 

 steps: counter,

 time: Timeperiod /1000,

  speed: SpeedRecord,

  });

推荐答案

如果您不希望在伪数字键中使用推送ID,请调用set而不是push.

If you don't want the push ID inside your pseudo-numeric keys, call set instead of push.

所以:

firebase.database().ref('testdata/User1').child(num).set({
  x: posX,
  y: posY,
  MaxSpeed: maxSpeed, 
  steps: counter,
  time: Timeperiod /1000,
  speed: SpeedRecord,
});

您的其他问题似乎(不确定,因为您没有包括增量代码)是由于num是字符串这一事实引起的.如果确实如此,请使用:

Your other problems seems (it's impossible to be certain, since you didn't include the code for the increment) to come from the fact that num is a string. If that is indeed the case, increment it with:

num = String(parseInt(num) + 1);


在Firebase中使用这种数字键是一种反模式,因此我通常建议不要使用它们.如果需要,请至少将它们填充到一定长度,以便您可以轻松地对它们进行排序/过滤.


Using such numeric keys is an antipattern in Firebase though, so I'd usually recommend against using them. If you must, at least pad them til a certain length, so that you can sort/filter on them easily.

简单的事情:

num = String(parseInt(num) + 1).padLeft(5, "0");

将在所有现代浏览器上运行,并确保所有看起来像数字但行为类似的字符串的键都将按您期望的顺序显示.

Will work on all modern browsers, and ensures that all keys-that-look-like-numbers-but-behave-like-strings will show up in the order you expect.

这篇关于将Firebase子项命名为序列数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆