简体   繁体   中英

Item-list inside of a JSON-object?

I am having a little problem.

Inside my MongoDB-collection, I have following record:

{
    "_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:

{ __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

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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