Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

Firebase orderByChild with startAt()'s second argument w/ pagination not ordering

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...