我使用本教程设置我的项目:http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/
我成功地可以将用户插入到数据库中,现在我正在尝试根据他们的ID(电子邮件)找到它们
db.get(' users')。find({email:req.params.id})返回一个Promise,函数进入" Sucessfully Found User,除了它实际上找不到什么?我试图获取此对象并将其作为响应返回为json
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest1');
router.get('/findUser/:id', function(req, res) {
var db = req.db;
db.get('users').find({email: req.params.id},
function(err, result) {
if (err) {
// If it failed, return error
res.send("There was an error finding user");
}
else {
res.send("Successfully Found User");
}
});
});
答案 0 :(得分:1)
尝试这样做!
db.get('users').find({email: req.params.id}, function(err, result) {
if (err) {
// If it failed, return error
res.send("There was an error finding user");
}
else {
res.send(result); // The result should be a JSON object that you can access with result.field (example)
}
});
这有意义吗?希望。