我可以获取火力点“查询”的第n个项目吗? [英] Can I get the nth item of a firebase "query"?

查看:161
本文介绍了我可以获取火力点“查询”的第n个项目吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase具有一些非常基本的查询功能,包括orderBy *,limitTo *,startAt ..等等。
有没有办法告诉Firebase你想得到第十个请求的结果?例如,我正在使用orderByKey。由于这些键是这些神奇的字符串,所以你不能用整数来引用它们中的任何位置。我想存储一个指向键位置的指针并在其中移动。我想orderByKey,并随意得到关键N.这是可能的吗?

解决方案

虽然您不能通过索引与Firebase访问子项目,你可以存储一个项目的关键,并使用它开始下一个查询。

  var ref = new Firebase('https ://yours.firebaseio.com/items'); 
var lastKnownKey = null;
var firstQuery = ref.orderByKey()。limitToFirst(100);
firstQuery.once('value',function(snapshot){
snapshot.forEach(function(childSnapshot){
lastKnownKey = childSnapshot.key();
});
});

现在你有一个变量 lastKnownKey 你见过的最后一把钥匙为了获得下一批孩子,你将这个值传递给 startAt()

  var nextQuery = ref.orderByKey()。startAt(lastKnownKey).limitToFirst(100); 


Firebase has some very basic query functionality with orderBy*, limitTo*, startAt.., etc... Is there a way to tell Firebase you want the 10th result of a request? For example, I am using orderByKey. Since the keys are these awesome magical strings, you cannot use integers to reference any position within them. I would like store a pointer to a location in keys and move through it. I want to orderByKey, and arbitrarily get key N. Is this possible?

解决方案

While you cannot access child items by index with Firebase, you can store the key of an item and use that to start a next query.

var ref = new Firebase('https://yours.firebaseio.com/items');
var lastKnownKey = null;
var firstQuery = ref.orderByKey().limitToFirst(100);
firstQuery.once('value', function(snapshot) {
  snapshot.forEach(function(childSnapshot) {
    lastKnownKey = childSnapshot.key();
  });
});

Now you have a variable lastKnownKey that has the last key you've ever seen. To get the next batch of children, you pass that value in to startAt():

var nextQuery = ref.orderByKey().startAt(lastKnownKey).limitToFirst(100);

这篇关于我可以获取火力点“查询”的第n个项目吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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