当前位置: 动力学知识库 > 问答 > 编程问答 >

json - Express.js - Mongo object returns Undefined

问题描述:

I am trying to get JSON from Mongodb using Express.js, and it returns "Undefined" in my console. Could you advise?

app.js:

var db = monk('localhost:27017/nodetest1', {

username : 'USERNAME',

password : 'PASSWORD'

});

index.js:

router.get('/url', function(req,res){

var db = req.db;

var collection = db.get('test1');

collection.find({},{},function(e,docs){

console.log(docs) // Returns "Undefined"

res.send(docs);

});

});

网友答案:

Check the "e" object, probably you have an error

router.get('/url', function(req,res){
  var db = req.db;
  var collection = db.get('test1');

  collection.find({},{},function(e,docs){
    if (!e) {
      console.log(docs); // Data you supposed to get in a correct way
    } else {
      console.log(e); // Checking the error - connection failure, bad authentication, etc.
    }
  });
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: