javascript - Firebase orderByKey() 键的顺序不一致

标签 javascript firebase angularfire

我必须满足一些业务要求,我的 key 部分由 Firebase Web api(例如 -JzuZoAtZ9fKY9z3GbEU)创建,部分由手动创建(例如 304810429574385)

我使用的 Firebase 版本:"version": "2.2.7"

我正在使用 orderByKey() 查询对项目进行排序,如 documentation points out :

  1. Children with a key that can be parsed as a 32-bit integer come first, sorted in ascending order.
  2. Children with a string value as their key come next, sorted lexicographically in ascending order.

有人可以告诉我为什么 firebase 生成的键不被视为普通字符串,并且它们的位置似乎像数字一样不可预测吗?

两个单独端点的排序键示例:

// case no 1
-JzLOQGFCw6ZwHnQZPFR
-JzuZoAtZ9fKY9z3GbEU
163467300371184
222032364474376
386146869432

// case no 2
222441849
225215973
225484186
-JzpekQUr7jRDHo5qk3D

Firebase 数据案例链接 FIRSTSECOND

JSfiddle orderByKey() usage.

先前的输出是使用以下方式打印的:

var keys = [];
fireRef.orderByKey().on('child_added',function(snap){
    keys.push(snap.key());
});

通过 REST api 获取数据时是一样的,使用 orderBy="$key" 订购时:

GET https://my_app.firebaseio.com/one/two/three.json?orderBy="$key"&auth=superSecret

如果使用 AngularFire,则相同:

$scope.items = $firebaseArray(fireRef.orderByKey());

最佳答案

您的情况 1 中的数字大于 what fits in a 32 bit integer :2147483647

如果将两个列表放在一起,会发生更明显的情况:

222441849
225215973
225484186
-JzLOQGFCw6ZwHnQZPFR
-JzuZoAtZ9fKY9z3GbEU
163467300371184
222032364474376
386146869432

推送 ID 之前的数字(以 -J 开头的键)按其数值排序。推送 ID 之后的数字不适合 32 位整数,因此(如您引用的文档中所述)是 ordered by their lexicographic value .

关于javascript - Firebase orderByKey() 键的顺序不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32742832/

相关文章:

ios - firebase 不总是通过 APNs 发送通知吗?

javascript - Firebase - 获取包含的所有数据

authentication - 用户通过电子邮件和密码身份验证登录到Firebase后,是否可以使用Google Auth链接用户Youtube视频?

javascript - .remove() 之后的 d3 回调函数

javascript找到对称差异

javascript - 如何计算文本的基线

android - 如何从 Firebase 检索所有数据并将其放在 RecyclerView 上

ios - 为当前用户 Firebase 创建静态变量?

javascript - AngularJS 未知提供程序错误(Firebase 和 AngularFire)

javascript - 两个 amCharts 图表未在 iPad 上使用 jQueryMobile 框架显示在同一页面上