Angularfire - 火力地堡查询/同步第一个X文件的项目 [英] Angularfire - Firebase query / sync first x items in document

查看:202
本文介绍了Angularfire - 火力地堡查询/同步第一个X文件的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚进入使用火力。我有6条通过ID索引的文档。
我想查询和同步列表中的第3项。

I am just getting into using firebase. I have a document with 6 records indexed by id. I would like to query and sync the first 3 items in the list.

var metals = new Firebase("https://<dbname>.firebaseio.com/metals").limit(3);
$scope.metals = $firebase(metals);

这提供了最后3项在列表中。不是我想要的东西。

This provides the last 3 items in the list. Not what I wanted.

因此​​,要获得的第一个第3项,我认为这会工作,但它打破...

So to get the first 3 items, I thought that this would work, but it breaks...

var metals = new Firebase("https://<dbname>.firebaseio.com/metals").startAt(1).endAt(3);
$scope.metals = $firebase(metals);

我的数据结构:

{
  "metals" : [ null, 
    {
      "id" : 1,
      "metal" : "Aluminium",
      "element" : "Au" 
    }, {
      "id" : 2,
      "metal" : "Silver",
      "element" : "Ag"
    }, {
      "id" : 3,
      "metal" : "Copper",
      "element" : "Cu"
    }, {
      "id" : 4,
      "metal" : "Tin",
      "element" : "Sn"
    }, {
      "id" : 5,
      "metal" : "Nickel",
      "element" : "Ni"
    }, {
      "id" : 6,
      "metal" : "Iron",
      "element" : "Fe"
    }]
}

HTML视图:

HTML view:

推荐答案

传递到第一个参数 startAt 是一个优先事项,而不是一个记录键。因为它看起来并不像你需要在这种情况下,密钥或优先级,你可以简单地这样的语法抽丝前三项:

The first parameter passed into startAt is a priority, not a record key. Since it doesn't look like you need a key or a priority in this case, you can simply this syntax to snag the first three items:

new Firebase(URL).startAt().limit(3)

此外,在ID存储在本例中的每个数据记录是不是该记录的关键,它的数据只是一个领域,很可能会成为富或任何其他任意名字,至于查询被关注到。

Additionally, the "id" stored in each record of the example data is not the key for that record, it's just another field in the data and might as well be "foo" or any other arbitrary moniker, as far as querying is concerned.

当这些记录获取存储在火力地堡,阵列被转换为一个对象,并且指数用作每个条目的键。因此,第一条记录的ID是0,而不是1所以,如果我想(通过3例如,1,你未遂)抓取到第四记录第二,我可以这样做:

When these records get stored in Firebase, the array is converted to an object, and the indices are used as the keys for each entry. So the first record's id would be 0, not 1. So if I wanted to grab the second through fourth records (e.g. 1 through 3 as you attempted), I could do this:

new Firebase(URL).startAt(null, 1).limit(3)

请有数据 doc和我们的博客的名单彻底读在阵列的最佳做法;大多数时候,你要避免这些,用唯一的ID。

Please have a thorough read of the lists of data doc and our blog on array best practices; most of the time, you'll want to avoid those and use unique ids.

这篇关于Angularfire - 火力地堡查询/同步第一个X文件的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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