2014-12-15 3 views
3

Я пытаюсь получить JSON из Mongodb с помощью Express.js, и он возвращает «Undefined» в моей консоли. Не могли бы вы посоветовать?Express.js - возвращает объект Mongo Undefined

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); 
    }); 
}); 

ответ

3

Проверьте "е" объект, вероятно, у вас есть ошибка

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. 
    } 
    }); 
}); 
+0

Вы можете проверить для неопределенных переменных как описано в http://stackoverflow.com/questions/2647867/how-to-determine-if-variable-is-undefined-or-null –