Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 尝试从Mongoose数据库接收值返回未定义?_Node.js_Mongodb - Fatal编程技术网

Node.js 尝试从Mongoose数据库接收值返回未定义?

Node.js 尝试从Mongoose数据库接收值返回未定义?,node.js,mongodb,Node.js,Mongodb,我一直在尝试从一个Mongoose数据库中获取密码属性,该数据库是我为存储用户而创建的。我可以很好地获取用户名,但当我尝试使用此项获取密码时: User.find({ 'credentials.username': gusername }, function(err, user) { if (err) throw err; console.log(user); console.log(user.credentials.password); }); 它将此记录在控制台中: Connected t

我一直在尝试从一个Mongoose数据库中获取密码属性,该数据库是我为存储用户而创建的。我可以很好地获取用户名,但当我尝试使用此项获取密码时:

User.find({ 'credentials.username': gusername }, function(err, user) {
if (err) throw err;
console.log(user);
console.log(user.credentials.password);
});
它将此记录在控制台中:

Connected to the DB using Mongo!
GET /login 200 66.097 ms - 1931
GET /stylesheets/style.css 304 2.212 ms - -
drew
[ { credentials: { password: 'tueresputa99', username: 'drew' },
__v: 0,
_id: 569e5e99e7810411003c4c25 } ]
/home/ubuntu/workspace/routes/index.js:127
console.log(user.credentials.password);
                            ^

TypeError: Cannot read property 'password' of undefined
at Query.<anonymous> (/home/ubuntu/workspace/routes/index.js:127: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/index.js:109:16
at doNTCallback0 (node.js:417:9)
at process._tickCallback (node.js:346:13)
[nodemon] app crashed - waiting for file changes before starting...
使用Mongo连接到数据库!
获取/登录200 66.097 ms-1931
GET/stylesheets/style.css 304 2.212 ms--
画
[{凭证:{密码:'tueresputa99',用户名:'drew'},
__v:0,
_id:569e5e99e7810411003c4c25}]
/home/ubuntu/workspace/routes/index.js:127
console.log(用户.凭证.密码);
^
TypeError:无法读取未定义的属性“password”
在询问时。(/home/ubuntu/workspace/routes/index.js:127:33)
at/home/ubuntu/workspace/node_modules/mongoose/node_modules/kareem/index.js:177:19
在/home/ubuntu/workspace/node_modules/mongoose/node_modules/kareem/index.js:109:16
在doNTCallback0(node.js:417:9)
在进程中调用(node.js:346:13)
[nodemon]应用程序崩溃-正在等待文件更改,然后再启动。。。
如您所见,它成功地记录了“user”,您可以在那里看到密码和用户名,但我无法使用
user.credentials.password


感谢您的帮助!这非常令人沮丧:/

您的
用户
回调
函数中是一个数组,而不是一个对象。注意,用户对象前面的JSON数组方括号
->[您的
用户
函数的
回调
内部是一个数组而不是一个对象。请注意,用户对象前面的JSON数组方括号
->[因此,我必须使用
User.findOne({'credentials.username':gusername},函数(err,User){
,然后我可以使用
User.credentials.password
?因此,我必须使用
User.findOne({'credentials.username':gusername},函数(err,User)){
,然后我可以使用
user.credentials.password