【问题标题】:In nodejs collection.find() is not fetching values from Mongodb collection.It returning nothing在 nodejs 中,collection.find() 没有从 Mongodb 集合中获取值。它什么也不返回
【发布时间】:2018-02-27 18:09:24
【问题描述】:

app.get('/render', function(req, res) {
  MongoClient.connect(dburl, function(err, db) {
  if (err) throw err;
  var collection = db.collection('shopping_list');
  collection.find().toArray(function(err, result) {
    
    res.send({result :result});
  
  });
  db.close();
});
});
在 nodejs 中,collection.find() 没有从 Mongodb 集合中获取值。它什么也不返回 在 nodejs 中,collection.find() 没有从 Mongodb 集合中获取值。它什么也不返回

【问题讨论】:

  • toArray() 不需要,而是使用 .exec(function(err,result){}) 因为 find 总是返回来自 mongodb 的对象数组

标签: javascript html node.js mongodb


【解决方案1】:
exports.index = function(req, res) {
var queryObj = {};
UserModel.find(queryObj)
.exec(function(err, users) {
    if (!users) {
        console.log("users not found");
    }
    if (!err) {
        console.log("number of users",users.length);
    } else {
        console.log("err",err);
    }
});

};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 2012-07-24
    • 2013-02-14
    • 2020-05-13
    相关资源
    最近更新 更多