开发者

How to findAll in mongoosejs?

My code is like that:

SiteModel.find(
    {},
    function(docs) {
        next(nu开发者_StackOverflow中文版ll, { data: docs });
    }
);

but it never returns anything... but if I specify something in the {} then there is one record. so, how to findall?


Try this code to debug:

SiteModel.find({}, function(err, docs) {
    if (!err) { 
        console.log(docs);
        process.exit();
    }
    else {
        throw err;
    }
});


The 2017 Node 8.5 way

try {
  const results = await SiteModel.find({});
  console.log(results);
} catch (err) {
  throw err;
}


From the documentation:

let result = SiteModel.find({}, function (err, docs) {});

or using async await you can do like this also:

let result = await SiteModel.find({});


const result = await SiteModel.find() - Without the {} in the .find() function works as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜