玉版印刷猫鼬系列;只拿到一份文件

Printing mongoose collections in jade template; only getting one document

本文关键字:文件 一份 系列      更新时间:2023-09-26

我是一般的web开发新手,想知道为什么游标方法只返回集合中的第一个文档,而不是全部?

我的目标是将这些文档传递给一个jade模板。

我正在使用MongoDB和Express

代码:

var array = [];
var stream = MyModel.find({}).cursor();
stream.on('data',function(task){
    array.push(task)
});
stream.on('error', function(err){
    //Handle error
});
stream.on('close', function(){
res.render('dashboard', { title: 'Dashboard', csrfToken: req.csrfToken(), array: array});
});

由于我是新手,我的"代码术语"可能不太准确,所以如果有必要,我很乐意说明。

你可以试试

MyModel.find({},function(err,docs){
 res.render('dashboard', { title: 'Dashboard', csrfToken: req.csrfToken(), array: docs});
});

根据mongoose doc,您可以为查询附加一个回调,这样它将把光标转换为文档数组。