【发布时间】:2017-03-23 04:34:32
【问题描述】:
我可以在控制台中看到填充的值,但是当我尝试访问它时,我得到了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.Name 是undefined!
有什么线索吗?使用猫鼬版本 4.6.6
【问题讨论】:
-
检查key是否有空格
-
你能显示国家模式吗?
-
@chridam 这似乎不是问题所在。
JSON.stringify(profile.countryId)的输出是{"_id":"57a9d5cda3c91dda14eb50f0","Name":"UK"} -
@RizwanJamal 完成
标签: javascript node.js mongodb mongoose-populate