0
我有一點問題。JSON對象內的項目列表?
在我的MongoDB的收集,我有以下記錄:
{
"_id" : ObjectId("58a77186e8b26df363d40195"),
"email" : "[email protected]",
"password" : "password123",
"fullName" : "Ola",
"interests" : ["football", "baseball"],
"__v" : 0
}
我想解析出 「利益」 雙組分,但是當我嘗試下面的代碼
var data = user;
console.log(data.interests);
。 ..我剛剛收到「未定義」。
當我嘗試到CONSOLE.LOG(數據)我收到:
{ __v: 0,
interests: [ "football", "baseball" ],
fullName: 'Ola',
password: 'password',
email: '[email protected]',
_id: 58a77186e8b26df363d40195 }
是否有任何人誰可以看到這個問題?我真的很新鮮。我使用Node.js的
UPDATE
這是一個通過回調發送數據對象的功能:
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);
}
什麼,當你使用括號記號'的console.log情況(數據[ 「利益」])' –
什麼的console.log的'輸出(數據[0])' –
原因有兩個:要麼是'JSON字符串'而不是'JSON對象',要麼'你有'對象數組'而不是'直接'對象'。 –