我在数据库上有20多个产品,我试图显示所有这些产品,所以我使用了
Product.find({}).toArray(function(err,data){
if(err){
res.send(err)
}
if(data){
res.send(data)
}
}但我犯了个错误
TypeError: Product.find(...).toArray is not a function所以我用了
Product.find({},function(err,products){
if(err){
res.send(err)
}
if(products){
res.send(products)
}
})但它只印出20种产品。所以我试着
Product.find({},function(err,products){
if(err){
res.send(err)
}
if(products){
res.send(products)
}
}).limit(300)但它仍然打印出20种产品
发布于 2019-06-13 19:52:46
使用承诺而不是使用回调
试试这个:
Products.find().then(products => {
res.send({ products })
}).catch(err => {
res.send({ err })
})它应该检索所有的产品,而不是只检索20个
如果只检索20,请检查使用.count()方法有多少
发布于 2019-06-13 20:36:35
您应该在limit -call之前添加类似-call的选项。此外,我假设包含的mongodb库设置了默认限制。
此代码示例将给您300个产品:
Product
.find({})
.limit(300)
.toArray(function(err,data) {
if (err) {
res.send(err);
} else if (data) {
res.send(data);
}
)};有关参考资料,请参阅mongodb-本机#查找和/或mongodb-原生游标
https://stackoverflow.com/questions/56587521
复制相似问题