查找方法mango + node.js访问值

时间:2013-07-15 15:18:01

标签: node.js mongodb mongoose

始终返回未定义的值(此处为user.password)。我真的不明白为什么

var userModel = restful.model('user');
userModel.find({email:req.params.email}, function(err, user){
    if (user.length){
      console.log(user); // WORKS WELL. I see whole object
      console.log(user.password); // Retuns undefined
      res.render(viewPath + "accountVerified", { meta: meta, success:1});
    }
});

1 个答案:

答案 0 :(得分:0)

您正在从db加载多个记录,但尝试从数组中访问变量。见编辑代码:

var userModel = restful.model('user');
userModel.find({email:req.params.email}, function(err, users){
    if (users.length) {
      console.log(users); // WORKS WELL. I see whole object
      console.log(users[0].password); // Retuns undefined
      res.render(viewPath + "accountVerified", { meta: meta, success:1});
    }
});