Mongoose:无法访问填充值

时间:2016-11-09 11:19:35

标签: javascript node.js mongodb mongoose-populate

我可以在控制台中看到填充的值,但在尝试访问它时我得到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

有任何线索吗?使用mongoose版本4.6.6

1 个答案:

答案 0 :(得分:2)

在Schema中,您已将字段“name”定义为“Name” 尝试获取profile.countryId.name