收藏
回答

云函数突破数据库每次只返回20条数据的限制,上滑刷新?

我的方法成功处理了每次只返回20条数据的限制,但是上滑加载更多的时候,为什么加载出来的数据是重复第一次的,而不是新数据啊?

云函数

async function getGoods() {
    const MAX_LIMIT = 100
    const countResult = await db.collection('Goods').count()
    const total = countResult.total
    const batchTimes = Math.ceil(total / 100)
    const list = []
    for (let i = 0; i < batchTimes; i++) {
      const promise = db.collection('Goods').aggregate()
        .lookup({
          from: 'User',
          localField: 'adminId',
          foreignField: '_id',
          as: 'adminInfo',
        })
        .skip(i * MAX_LIMIT).limit(MAX_LIMIT).end()
      list.push(promise)
    }
    return (
      await Promise.all(list)).reduce((acc, cur) => ({
      data: acc.data.concat(cur.data),
      errMsg: acc.errMsg,
    }))
  }


index.js

_fecthGoods: function () {
    wx.cloud.callFunction({
      name: 'goodsMgt',
      data: {
        req_type: 'Selection'
      }
    }).then(res => {
      console.log(res)
      let goodsList = res.result.list
      this.setData({
        goodsList: goodsList
      })
      wx.lin.renderWaterFlow(goodsList);//瀑布流组件
      console.log("商品列表:", this.data.goodsList)
    })
  },

onReachBottom: function () {
    this._fecthGoods()
    wx.stopPullDownRefresh({
      success: (res) => {
        console.log(res)
      },
    })
  },
回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容
问题标签