Node.js 如何访问Mongoose模式属性?

Node.js 如何访问Mongoose模式属性?,node.js,mongodb,Node.js,Mongodb,我对使用Mongoose和MongoDB相当陌生。我正在注册/登录页面上工作。register函数工作正常,将用户帐户存储到数据库中,但我的问题是登录。我试图做的是从数据库中的匹配用户那里获取“password”属性,以匹配用户输入的密码。这是我的登录功能 router.post('/logSubmit', function(req, res, next) { var gusername = req.body.username; var gpassword = req.body.password

我对使用Mongoose和MongoDB相当陌生。我正在注册/登录页面上工作。register函数工作正常,将用户帐户存储到数据库中,但我的问题是登录。我试图做的是从数据库中的匹配用户那里获取“password”属性,以匹配用户输入的密码。这是我的登录功能

router.post('/logSubmit', function(req, res, next) {
var gusername = req.body.username;
var gpassword = req.body.password;

User.count({
    'credentials.username': gusername
}, function(err, count) {
    if (err) throw err;
    console.log(count);
    if (count > 0) {

      // Where I need to pull password attribute from the database

    } else {

      // Wrong username or password

    }
});
});
我在互联网上搜索过如何从数据库条目中读取属性,但什么也找不到。我觉得这很简单,但我想我不知道语法。我的模型的名称是User。我想应该是这样的:

User.find({ username: gusername }, function(err, user) {
if (err) throw err;

var getpassword = user.password;
console.log(getpassword);

});
我有点认为那会管用,但事实并非如此。如何从数据库访问密码属性??谢谢

编辑:

这是存储在我的数据库中的我的用户帐户的外观:

{
"_id": {
    "$oid": "569e5344d4355010b63734b7"
},
"credentials": {
    "username": "testuser",
    "password": "password1234"
},
"__v": 0
}

一个
find
查询就足够了。如果从
find
查询中检索到非null的
user
对象,则可以保证该对象是具有密码的用户

User.find({ 'credentials.username': gusername }, function(err, users) {
  if (err) throw err;

  // 'users' is an array of the User objects retrieved.
  users.forEach(function(user) {
    // Do something with the password.
    // The password is stored in user.credentials.password
    console.log(user.credentials.password);
  });
});

一个
find
查询就足够了。如果从
find
查询中检索到非null的
user
对象,则可以保证该对象是具有密码的用户

User.find({ 'credentials.username': gusername }, function(err, users) {
  if (err) throw err;

  // 'users' is an array of the User objects retrieved.
  users.forEach(function(user) {
    // Do something with the password.
    // The password is stored in user.credentials.password
    console.log(user.credentials.password);
  });
});

嗯,我想这取决于在mongoose中如何定义
User
。你能粘贴你的模式定义吗?
var userSchema=newschema({凭证:{用户名:字符串,密码:字符串}});var User=mongoose.model('User',userSchema);module.exports=用户var getpassword=user.credentials.password?我试过了,得到了
TypeError:无法读取未定义的属性“password”
,我想这取决于
User
在mongoose中是如何定义的。你能粘贴你的模式定义吗?
var userSchema=newschema({凭证:{用户名:字符串,密码:字符串}});var User=mongoose.model('User',userSchema);module.exports=用户var getpassword=user.credentials.password?我尝试了一下,得到了
TypeError:无法读取未定义的
console.log(user.credentials.password)的属性“password”;^TypeError:无法在查询时读取未定义的属性“password”。(/home/ubuntu/workspace/routes/index.js:124:33)at/home/ubuntu/workspace/node_modules/mongoose/node_modules/kareem/index.js:177:19 at/home/ubuntu/workspace/node_modules/mongoose/node_modules/kareem.js:109:16 at doNTCallback0(node.js:417:9)at process.\n.js:346:13)[nodemon]应用程序崩溃-在启动前等待文件更改…很抱歉格式设置太糟糕,但这就是我的控制台打印我已经纠正了解决方案中的错误。我对这个错误表示歉意。您可以通过将文本放在反勾之间(看起来像
但倾斜得更多的特殊字符)来格式化文本。您还可以单击注释框右侧的
帮助
选项卡,了解如何使用Markdown.console.log(user.credentials.password);^TypeError:无法在查询时读取未定义的属性“password”。(/home/ubuntu/workspace/routes/index.js:124:33)at/home/ubuntu/workspace/node_modules/mongoose/node_modules/kareem/index.js:177:19 at/home/ubuntu/workspace/node_modules/mongoose/node_modules/kareem.js:109:16 at doNTCallback0(node.js:417:9)at process.\n.js:346:13)[nodemon]应用程序崩溃-在启动前等待文件更改…很抱歉格式设置太糟糕,但这就是我的控制台打印我已经纠正了解决方案中的错误。我对这个错误表示歉意。您可以通过将文本放在反勾之间(看起来像
但倾斜得更多的特殊字符)来格式化文本。您还可以单击注释框右侧的
帮助
选项卡,了解如何使用标记设置文本格式。