Firestore startAfter()按时间戳降序以无限滚动方式返回相同的数据 [英] Firestore startAfter() returning the same data in infinite scrolling when ordered by descending timestamp

查看:108
本文介绍了Firestore startAfter()按时间戳降序以无限滚动方式返回相同的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过在Ionic上使用Firestore通过将 orderBy ()设置为" timestamp " ,使用时间顺序的用户帖子(顶部的最新帖子)来写一个个人资料页面降.当用户到达底部时,我正在使用Ionic的无限加载功能来加载更多帖子,但是结果是Firestore会一遍又一遍地加载完全相同的帖子.请帮忙!

I'm writing a profile page with chronological user posts (latest post on top) using Firestore on Ionic by setting orderBy() to "timestamp" descending. I'm using Ionic's infinite loading to load more posts when the user reaches the bottom, but the result is that Firestore loads the exact same posts over and over again. Please help!

嗨!

很抱歉,如果这是一个初学者的问题,但是我已经花了好几个小时来解决这个问题,但无济于事.当按升序排列时,分页可以正常工作,但按降序排列时,分页可以加载相同的帖子.我已经考虑过尝试以下替代方法,但它们并不划算:

Sorry if this is a beginner question, but I've been wrapping my head around this for sever hours to no avail. The pagination works properly when in ascending order, but loads the same posts when in descending order. I've looked at trying the following alternatives, but they would not be cost-efficient:

  1. 在用户到达最低点时更改限制:这将导致一遍又一遍地阅读所有帖子
  2. 以升序执行但反转数组:将破坏分页的目的
  3. 使用查询(位置)在x时间戳记之前抓取文档:理论上可行,但有点hacky,我真的想知道startAfter()的工作原理,因为它已经存在.

.

'''

READ_posts__profile(uid,limit, start) : Promise<[]>
  {
      return new Promise((resolve, reject) =>
      {

         if (start == null)
         {
            resolve();
         }
        else if(start == 'head')
        {
            start = new Date();
        }
            console.log(start);


         this._DB.collection(this.ref.posts + uid + '/userposts' )
         .orderBy("timestamp", 'desc').startAfter(start).limit(limit)
         .get()
         .then((querySnapshot) =>
         {

            let obj : any = [];

            console.log(obj);

            querySnapshot
            .forEach((doc : any) =>
            {
               obj.push({
                  id             : doc.id,
                  content        : doc.data().content,
                  likes          : doc.data().likes,
                  timestamp      : doc.data().timestamp,
                  comments       : doc.data().comments
               });

            });

            resolve(obj);
         })
         .catch((error : any) =>
         {
            reject(error);
         });
      });
  }

'''

预期结果:帖子从最新到最旧的无限滚动 结果:一次又一次无限滚动前x个帖子(限制为x个)

Expected result: Infinite scrolling of posts from latest to oldest Result: Infinite scrolling of first x posts again and again (limit is x)

感谢您抽出宝贵的时间阅读.希望很快能收到你们的来信!

Thank you for taking the time to read. Hope to hear from you guys soon!

将来的参考更新: 与其在.startAfter()中使用doc本身,还不如在doc.timestamp中使用它,如下所示:

UPDATE FOR FUTURE REFERENCE: Rather than using the doc itself in .startAfter(), it worked with using doc.timestamp like so:

      this._DB.collection(this.ref.posts + uid + '/userposts' )
  .orderBy("timestamp", "desc").startAfter(start.timestamp).limit(this.limit)
  .get()
  .then(querySnapshot =>
  {

推荐答案

就我而言,我添加了这样的查询

In my case I added the query like this

query.startAfter(lastId)

应该在的地方

query = query.startAfter(lastId)

这篇关于Firestore startAfter()按时间戳降序以无限滚动方式返回相同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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