2016-11-09 153 views
0

我可以看到在控制檯中填充值,但在嘗試訪問它時,我得到undefined貓鼬:不能訪問填充值

const AccountProfile = new Schema({ 
 
    owner: { type: String }, 
 
    firstName: { type: String }, 
 
    lastName: { type: String }, 
 
    countryId: { type: mongoose.Schema.Types.ObjectId, ref: "Country" } 
 
}); 
 

 
const Country = new Schema({ 
 
    name: String 
 
}); 
 

 
AccountProfile.statics.getProfile = function (username, cb) { 
 
    this.model("AccountProfile").findOne({owner: username}) 
 
    .populate("countryId") 
 
    .exec(function (err, profile) { 
 
     if (err) { 
 
      return cb(err); 
 
     } 
 
     console.log(profile.countryId); 
 
     return cb(null, profile); 
 
    }); 
 
};

輸出的格式如下: { _id: 57a9d5cda3c91dda14eb50f0, Name: 'UK' }

打印profile.countryId._id看起來不錯,但是profile.countryId.Nameundefined

任何線索?使用貓鼬4.6.6版

+0

檢查是否有在關鍵 – chridam

+0

任何空格能否請您顯示,目前我國的架構? –

+0

@chridam這似乎不是問題所在。 'JSON.stringify(profile.countryId)的輸出''是{ 「_id」: 「57a9d5cda3c91dda14eb50f0」, 「姓名」: 「英國」}' – ierax

回答

2

在架構定義了場「名」不「名」 嘗試與得到profile.countryId.name

+0

而是你如何在控制檯獲得 「姓名」 ...... .....如果在'Country'架構 –

+0

沒有定義如果您的問題已得到解決......可以請你接受這個答案和標記出:) –

+0

感謝。問題是,在實際的mongo文檔中,字段在我的代碼名稱中被稱爲'Name'(我在將現有數據導入到我想使用代碼訪問的mongo後創建了mongoose模式)。所以我不得不將我的字段在貓鼬模式中重命名爲「Name」以與mongo中的內容保持一致。這就解釋了爲什麼'Name'被打印在控制檯上 – ierax