簡體   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