繁体   English   中英

JSON对象中的项目列表?

[英]Item-list inside of a JSON-object?

我有一个小问题。

在我的MongoDB集合中,我有以下记录:

{
    "_id" : ObjectId("58a77186e8b26df363d40195"),
    "email" : "mail@mail.com",
    "password" : "password123",
    "fullName" : "Ola",
    "interests" : ["football", "baseball"],
    "__v" : 0
}


我想解析“兴趣”部分,但是当我尝试以下代码时

var data = user;
console.log(data.interests);

...我刚刚收到“未定义”。


当我尝试console.log(data)时,我收到:

{ __v: 0,
  interests: [ "football", "baseball" ],
  fullName: 'Ola',
  password: 'password',
  email: 'mail@mail.com',
  _id: 58a77186e8b26df363d40195 }

有没有人可以看到问题? 我真的很新。 我正在使用Node.JS

更新
这是通过回调发送数据对象的函数:

  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);
  }

我看不到问题在哪里。 我使用了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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM