Firebase结果范围使用startAt和endAt [英] Firebase results range using startAt and endAt

查看:214
本文介绍了Firebase结果范围使用startAt和endAt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的Firebase数据中获得前100个结果,然后是下100个,然后是下100个。我尝试了多种方法。

Version 1

  ref.child('products')。orderByChild('domain')。startAt(0).endAt 100).once('value')。then(function(snapshot){}); 

版本2

  ref.child('products')。orderByChild('domain')。startAt(0).limitToFirst(100).once('value')。then(function(snapshot){}); 

版本3

 < ('product')。startAt(0).endAt(100).once('value')。then(function(snapshot){}); code> ref.child 

每次返回的快照为空。无论如何,在Firebase中获取我之后的数据范围?

解决方案当您调用 orderByChild()时,任何后续调用 startAt() endAt() equalTo()你传给孩子的价值。你正在传递一个索引。除非 domain 的值是一个顺序索引(极不可能),否则这将不起作用。

do记住你得到的最后一个孩子的值,然后把它传递给 startAt()下一个页。

  var productsRef = ref.child('products'); 
var lastKnownDomainValue = null;
var pageQuery = productsRef.orderByChild('domain')。limitToFirst(100);
pageQuery.once('value',function(snapshot){
snapshot.forEach(function(childSnapshot){
lastKnownDomainValue = childSnapshot.child('domain')。val();
});
});

现在你有一个变量 lastKnownDomainValue 你见过的最后一个域名。为了获得下一批孩子,你将这个值传递给 startAt()

  var nextQuery = productsRef.orderByChild('domain')。startAt(lastKnownDomainValue).limitToFirst(100); 

然后当你循环时更新 lastKnownDomainValue 再次给孩子们。


I'm trying to get the first 100 results from my Firebase data, then the next 100, then the next 100 etc. I've tried multiple ways.

Version 1

ref.child('products').orderByChild('domain').startAt(0).endAt(100).once('value').then(function(snapshot) {});

Version 2

ref.child('products').orderByChild('domain').startAt(0).limitToFirst(100).once('value').then(function(snapshot) {});

Version 3

ref.child('products').startAt(0).endAt(100).once('value').then(function(snapshot) {});

Every time the snapshot returned is null. Is there anyway in Firebase of getting the range of data I'm after?

解决方案

When you call orderByChild() any subsequent call to startAt(), endAt() or equalTo() expects the value that you pass in to be a child. You're passing in an index. Unless the value of domain is a sequential index (highly unlikely), this will not work.

What you should do is remember the domain value of the last child you got, and then pass that in to startAt() for the next "page".

var productsRef = ref.child('products');
var lastKnownDomainValue = null;
var pageQuery = productsRef.orderByChild('domain').limitToFirst(100);
pageQuery.once('value', function(snapshot) {
  snapshot.forEach(function(childSnapshot) {
    lastKnownDomainValue = childSnapshot.child('domain').val();
  });
});

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

var nextQuery = productsRef.orderByChild('domain').startAt(lastKnownDomainValue).limitToFirst(100);

And then you update lastKnownDomainValue when you loop over the children again.

这篇关于Firebase结果范围使用startAt和endAt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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