首页 > 解决方案 > 用快递分页

问题描述

我正在使用 mongoose & express 进行分页,这是代码

 const page = req.query.page || 1;
 const limit = 15;
 const docs = await Model.find({
    visibility: true,  
    $or: [{expires: {$gt: new Date().getTime()}}, {expires: null}]
 })
 .skip((page * limit) - limit)
 .limit(limit);

这会导致错误(没有消息它只记录网络错误)但我知道问题的原因是这段代码,因为当我删除它时,它开始工作。什么是问题?

编辑

我将此代码更改为

Model.find({
    visibility: true,  
    $or: [{expires: {$gt: new Date().getTime()}}, {expires: null}]
})
.skip(page * limit - limit)
.limit(15)
.exec((err,docs)=>{
    if(err){
       //handle err
    }
    res.status(200).send({
         docs
    })
})

它开始工作了。听起来不错,但我需要知道为什么它不能与 await 语法一起使用

标签: node.jsmongodbexpressmongoose

解决方案


推荐阅读