我正在尝试使用Express.js从Mongodb获取JSON,并且它在我的控制台中返回“未定义”。你能建议吗?
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);
});
});
检查“ e”对象,可能您有一个错误
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.
}
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句