Javascript Mongoose:无法访问填充的值

Javascript Mongoose:无法访问填充的值,javascript,node.js,mongodb,mongoose-populate,Javascript,Node.js,Mongodb,Mongoose Populate,我可以在控制台中看到填充的值,但是当尝试访问它时,我得到了未定义的: const AccountProfile=新架构({ 所有者:{type:String}, 名字:{type:String}, 姓氏:{type:String}, countryId:{type:mongoose.Schema.Types.ObjectId,ref:“Country”} }); const Country=新模式({ 名称:String }); AccountProfile.statics.getProfil

我可以在控制台中看到填充的值,但是当尝试访问它时,我得到了未定义的

const AccountProfile=新架构({
所有者:{type:String},
名字:{type:String},
姓氏:{type:String},
countryId:{type:mongoose.Schema.Types.ObjectId,ref:“Country”}
});
const Country=新模式({
名称:String
});
AccountProfile.statics.getProfile=函数(用户名,cb){
this.model(“AccountProfile”).findOne({owner:username})
.populate(“countryId”)
.exec(函数(错误,配置文件){
如果(错误){
返回cb(错误);
}
console.log(profile.countryId);
返回cb(空,配置文件);
});

};在模式中,您定义了字段“name”而不是“name”
尝试获取
profile.countryId.name

检查键中是否有空格。是否可以显示国家架构?@chridam似乎没有问题。
JSON.stringify(profile.countryId)
的输出是
{“\u id”:“57a9d5cda3c91dda14eb50f0”,“Name”:“UK”}
@RizwanJamal Donebut您如何在控制台中获得“Name”。。。。。。。。如果未在
国家/地区
方案银行中定义。问题是,在实际的mongo文档中,该字段被称为
Name
,而在我的代码
Name
(我在将现有数据导入mongo之后创建了mongoose模式,我想使用代码访问这些数据)。因此,我不得不将mongoose模式中的字段重命名为
Name
,以与mongo中的字段保持一致。这就解释了为什么控制台中会打印
Name