[英]Item-list inside of a JSON-object?
I am having a little problem. 我有一个小问题。
Inside my MongoDB-collection, I have following record: 在我的MongoDB集合中,我有以下记录:
{
"_id" : ObjectId("58a77186e8b26df363d40195"),
"email" : "mail@mail.com",
"password" : "password123",
"fullName" : "Ola",
"interests" : ["football", "baseball"],
"__v" : 0
}
I want to parse out the "interests"-part, but when I try the following code 我想解析“兴趣”部分,但是当我尝试以下代码时
var data = user;
console.log(data.interests);
...i just receive "Undefined". ...我刚刚收到“未定义”。
When I try to to console.log(data) I receive: 当我尝试console.log(data)时,我收到:
{ __v: 0,
interests: [ "football", "baseball" ],
fullName: 'Ola',
password: 'password',
email: 'mail@mail.com',
_id: 58a77186e8b26df363d40195 }
Is there anyone who can see the problem? 有没有人可以看到问题? Im really new to this. 我真的很新。 I am using Node.JS 我正在使用Node.JS
UPDATE 更新
This is the function that sends the data-object through a callback: 这是通过回调发送数据对象的函数:
try {
decoded = jwt.decode(token, config.secret);
User.findOne({
email: decoded.email
}, function(err, user) {
if (err) throw err;
if (!user) {
callback(false);
} else {
callback(true, user);
}
});
} catch (e) {
callback(false);
}
I don't see where problem is coming. 我看不到问题在哪里。 I used your obj to run code its working fine (except that you are getting it from database, I am assigning directly, but thats not a problem). 我使用了obj来运行其正常工作的代码(除了您是从数据库中获取代码,我是直接分配的,但这不是问题)。
var obj={ "_id" : "58a77186e8b26df363d40195", "email" : "mail@mail.com", "password" : "password123", "fullName" : "Ola", "interests" : ["football", "baseball"], "__v" : 0 }; //I removed ObjectId() because of error: "message": "Uncaught ReferenceError: ObjectId is not defined", for(var k in obj) { console.log(obj[k]); } //or directly doing it console.log(obj.interests);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.