【问题标题】:How to implement a "scroll to load more" using Node.js and MongoDB?如何使用 Node.js 和 MongoDB 实现“滚动加载更多”?
【发布时间】:2020-08-25 17:42:50
【问题描述】:

我有一个使用 Express+Typescript 的节点服务器,它使用 Mongoose 连接到 MongoDB 数据库。 在我的数据库中,我有一个名为 Products 的集合,前端(Angular 9)请求显示它们。

现在我只需发送一个GET 请求,我的服务器就会返回所有产品:

public async getAll() {
        const products = await this.productModel.find({}).then(products => products).catch(() => false)

        if (!products)
            throw new Error('Could\'nt get products')

        return products
    }

角度:

getAllProducts() {
    return this.http.get<IProduct[]>('http://127.0.0.1:3000/products/')
  }

我的问题是如何按“块”检索产品,例如每个请求的 n 产品也无需重复产品?

【问题讨论】:

    标签: javascript node.js angularjs mongodb mongoose


    【解决方案1】:

    您可以在查询中使用偏移量和限制发送限制和偏移量并在 MongoDB 上使用它 例如对于第 1 页和第 20 页数据,每页偏移 0 限制 20
    对于第 2 页和第 20 页数据,每页偏移 20 限制 20 依此类推

      await this.productModel.find({}).skip(offset).limit(limit).then(products => products).catch(()
        => false)
    

    【讨论】:

      猜你喜欢
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      相关资源
      最近更新 更多