【问题标题】:How to findAll in mongoosejs?如何在 mongoosejs 中查找所有内容?
【发布时间】:2011-11-07 00:47:47
【问题描述】:

我的代码是这样的:

SiteModel.find(
    {},
    function(docs) {
        next(null, { data: docs });
    }
);

但它从不返回任何内容...但是如果我在 {} 中指定某些内容,则只有一条记录。那么,怎么找到呢?

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    试试这段代码来调试:

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

    【讨论】:

    • 任何想法为什么当我从node filename.js这样的终端运行它时这不起作用?
    • 请注意,如果您使用 then-catch 之类的范例,您甚至不需要空花括号。
    【解决方案2】:

    const result = await SiteModel.find() - 在.find() 函数中没有{} 也可以。

    【讨论】:

      【解决方案3】:

      来自documentation

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

      或者使用 async await 你也可以这样做:

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

      【讨论】:

        【解决方案4】:

        2017 节点 8.5 方式

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

        【讨论】:

          猜你喜欢
          • 2013-10-23
          • 1970-01-01
          • 2016-03-14
          • 2015-08-24
          • 2013-05-23
          • 1970-01-01
          • 2011-01-19
          • 2021-11-29
          • 1970-01-01
          相关资源
          最近更新 更多