firebase orderByChild with startAt()的第二个参数w / pagination not odering [英] Firebase orderByChild with startAt()'s second argument w/ pagination not odering

查看:129
本文介绍了firebase orderByChild with startAt()的第二个参数w / pagination not odering的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Firebase调用,看起来像这样,

$ $ $ $ $ c $ ref.child(`````````````` ('value',(snapshot)=> {
var topPosts = [


$ limit $ ];
snapshot.forEach((childSnapshot)=> {
var post = childSnapshot.val();
post.key = childSnapshot.key();
topPosts。
this.lastPostKeyTop = topPosts [topPosts.length - 1] .key;
this.setState({
topPosts,
isLoading:false
});
});

现在我想要做的是当用户滚动到页面底部时,我调用这个函数。

  ref.child(`floorPosts / $ {this.props.floorName}`)
.orderByChild('numberOfLikes ')
.startAt(null,this.lastPostKeyTop)
.limitToLast(15)
.once('value',(snapshot)=> {
var topPosts = ];
snapshot.forEach((childSnapshot)=> {
var post = childSnapshot.val();
post.key = childSnapshot.key();
topPosts。
this.setState({
topPosts:this.state.topPosts.concat(topPosts),
isLoading:false,
});
});

但是,它将最初的15个结果连接在一起。 .startAt 需要第二个参数,这个(我相信)是从firebase取得的最后一个项目的关键字。但是,这似乎并没有在我的情况。我有一个预感,它是与 snapshot.forEach 代码有关的。



Tyler

解决方案

对于迟到的难以置信的回答泰勒



参数 startAt()仅用于消除与第一个参数匹配的项目之间的歧义。所以你必须同时传递 numberOfLikes 的值来从第一页最后一项的关键字(通常被称为锚项目)。另外请注意,这意味着下一组结果中的第一项将与前一组结果中的最后一项相同。所以你需要检索一个额外的项目。


I have a Firebase invocation which looks like this,

      ref.child(`floorPosts/${this.props.floorName}`)
        .orderByChild('numberOfLikes')
        .limitToLast(15)
        .once('value', (snapshot) => {
          var topPosts = [];
          snapshot.forEach((childSnapshot) => {
            var post = childSnapshot.val();
            post.key = childSnapshot.key();
            topPosts.unshift(post);
          });
          this.lastPostKeyTop = topPosts[topPosts.length - 1].key;
          this.setState({
            topPosts,
            isLoading: false
          });
      });

Now what I'm trying to do, is when the user scrolls to the bottom of the page, I invoke this function.

ref.child(`floorPosts/${this.props.floorName}`)
    .orderByChild('numberOfLikes')
    .startAt(null, this.lastPostKeyTop)
    .limitToLast(15)
    .once('value', (snapshot) => {
      var topPosts = [];
      snapshot.forEach((childSnapshot) => {
        var post = childSnapshot.val();
        post.key = childSnapshot.key();
        topPosts.unshift(post);
      });
      this.setState({
        topPosts: this.state.topPosts.concat(topPosts),
        isLoading: false,
      });
  });

However, it's concatenating the same first 15 results as it initially did. .startAt takes in a a second argument, which (I believe) is the key of the last item which was fetched from firebase the initial fetching. However, this doesn't seem to be working in my case. I have a hunch it's something to do with the snapshot.forEach code.

Tyler

解决方案

sorry for the incredibly late answer Tyler

The second argument to startAt() is only used to disambiguate between items that match the first argument. So you must pass in both the value of numberOfLikes to start at and the key of the last item on the first page (often referred to as the anchor item).

Also note that this means the first item of the next set of results will be the same as the last item in the previous set of results. So you'll need to retrieve one extra item.

这篇关于firebase orderByChild with startAt()的第二个参数w / pagination not odering的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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