本文介绍了Firebase orderByChild顺序不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 我的样本数据
-
代码
Code
let users_ref = firebase_instance.database().ref('users');
let amount_of_data = 4;
let limit_users_ref = users_ref.orderByChild("score").limitToLast(amount_of_data);
limit_users_ref.once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
console.log([childKey, childData]);
});
})
我得到的输出
Output I am getting
从输出中可以看出,数据没有排序。我应该在那里获得test1而不是test3。我做错了什么?
As it can be seen on output, the data is not ordered. I should be getting test1 in stead of test3 there. Anything I am doing wrong?
推荐答案
您的分数是字符串。它们应该是数字。字符串排序不像数字。
Your scores are strings. They should be numbers instead. Strings don't sort like numbers.
这篇关于Firebase orderByChild顺序不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!