Firebase排序数据检索 [英] Firebase Sorting Data Retrieval

查看:684
本文介绍了Firebase排序数据检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我有的代码:我正在尝试将按照工资排序的数据存储在Firebase数据库中。数据库的顺序是:Posts - > PostID(保存时为.push) - >工资。我的数据检索工作不正确。

  var PostsRef = firebase.database()。ref()。child(Posts); 
PostsRef.on('child_added',function(data){
var title = data.val()。title;
var description = data.val()。description;
var completeDate = data.val()。completionDate;
var complexity = data.val()。complexity;
var wage = data.val()。wage;
var uid = data .val()。userID;
});


解决方案

他们将按字典顺序排序:

$ block $ $ b $ 1

13 $ / p>

2



20

3


如果您想以数字顺序得到结果,您应该将工资存储为数字,所以不用引号。 b

如果您从代码中存储它们,可能需要使用 parseInt(salaryAsString)来转换它。



将工资存储为数字后,您可以按照以下顺序获得工资:
$ b $ pre $ var $ PostsRef = firebase.database()REF()子( 信息)。;
var query = PostsRef.orderByChild(wage);
query.on('child_added',function(data){


This is the code that I have: I am trying to get data stored in the Firebase Database sorted by wage. The order of the database goes: "Posts -> PostID(.push when saving) -> Wage." I have the data retrieval working just not in order.

var PostsRef = firebase.database().ref().child("Posts");
PostsRef.on('child_added', function(data) {
  var title = data.val().title;
  var description = data.val().description;
  var completionDate = data.val().completionDate;
  var complexity = data.val().complexity;
  var wage = data.val().wage;
  var uid = data.val().userID;
});

解决方案

You're storing the wages as string, which means that they will be sorted in lexicographical order:

1

13

2

20

3

If you want to get the results in numerical order, you should store the wages as numbers, so without the quotes.

If you store them from your code, this might require that you convert it with parseInt(wagesAsString).

After you store the wages as numbers, you can get them in order with:

var PostsRef = firebase.database().ref().child("Posts");
var query = PostsRef.orderByChild("wage");
query.on('child_added', function(data) {

这篇关于Firebase排序数据检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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