我如何只获得mongoose.paginate

时间:2019-04-29 12:40:49

标签: express mongoose mongoose-populate

我想找到ad.author是用户登录的地方的购买商品!但是在“购买”模型中,我有“广告”模型参考,当我需要找到它时,我需要在所有购买中进行填充,所以我想。问题是:当我使用mongoose.paginate时,结果不是“好”,因为当ad.author不同时,我得到的结果是ad:null ....

{
      "_id": "5cc6e3f30bca881bd1ade9af",
      "ad": null,
      "content": "Eu quero comprar",
      "interested": "5cc6e3a10bca881bd1ade9ae",
      "createdAt": "2019-04-29T11:45:55.061Z",
      "__v": 0
    },
    {
      "_id": "5cc6e4aa0bca881bd1ade9b1",
      "ad": {
        "_id": "5cc6e44f0bca881bd1ade9b0",
        "price": 300,
        "author": "5cc6e3a10bca881bd1ade9ae"
      },
      "content": "Eu quero comprar o seu curso, cara",
      "interested": "5cc4faacdd90ce534e5f184a",
      "createdAt": "2019-04-29T11:48:58.545Z",
      "__v": 0
    },
async index (req, res) {
    let results = []
    await Purchase.find()
      .populate('ad')
      .then(response =>
        response.map(el => {
          if (el.ad.author == req.userId) results.push(el)
        })
      )

    return res.json(results)
  }


async index (req, res) {
    const purchase = await Purchase.paginate(
      {},
      {
        page: parseInt(req.query.page) || 1,
        limit: 20,
        populate: {
          path: 'ad',
          match: { ad: { author: req.userId } },
          select: ['author', 'email', 'price']
        },
        sort: '-createAt'
      }
    )

    return res.json(purchase)
  }

我想要的是:

{
      "_id": "5cc6e4aa0bca881bd1ade9b1",
      "ad": {
        "_id": "5cc6e44f0bca881bd1ade9b0",
        "price": 300,
        "author": "5cc6e3a10bca881bd1ade9ae"
      },
      "content": "Eu quero comprar o seu curso, cara",
      "interested": "5cc4faacdd90ce534e5f184a",
      "createdAt": "2019-04-29T11:48:58.545Z",
      "__v": 0
    },

0 个答案:

没有答案