2016-11-09 65 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