我正在尝试猫鼬分页,但出现以下错误。
TypeError: this.count is not a function
at Function.paginate
这是我的代码:
Stock.paginate({},{page, limit} ,function(err,result){
if (err) {
return res.status(500).json({ message: 'Error occurred while fetching users.' });
}
const { docs, total, limit, page, pages } = result;
// console.log(docs)
return res.json({ stock: docs, total, limit, page, pages });
})
我已将该插件添加到我的 Stock Schema 中。
const StockSchema = new mongoose.Schema(
{
item: {
type: String,
},
quantity: {
type: Number,
}
}
);
StockSchema.plugin(mongoosePaginate)
// mongoosePaginate(StockSchema);
module.exports = mongoose.model("Stock", StockSchema);
请帮助我理解我可能出错的地方。
mongoose-paginate 没有计数方法。所以,最好使用 mongoose-paginate-v2
安装
npm install --save mongoose-paginate-v2 @types/mongoose-paginate
如果您使用打字稿编码,则需要@types/mongoose-paginate
有解决办法