Node.js Mongoose-从架构中访问填充字段

Node.js Mongoose-从架构中访问填充字段,node.js,mongoose,Node.js,Mongoose,我有一个引用另一个模型的模式。差不多 var bookSchema = new Schema({ title: String, series: { type: Schema.Types.ObjectId, ref: 'Series' } }); bookSchema.methods.fullTitle = function() { return [this.series.title, this.title].join(" - "); } 现在我在图书模式中有一个函数,

我有一个引用另一个模型的模式。差不多

var bookSchema = new Schema({
    title: String,
    series: { type: Schema.Types.ObjectId, ref: 'Series' }
});
bookSchema.methods.fullTitle = function() {
    return [this.series.title, this.title].join(" - ");
}
现在我在图书模式中有一个函数,需要访问该
系列
。我想做一些像

var bookSchema = new Schema({
    title: String,
    series: { type: Schema.Types.ObjectId, ref: 'Series' }
});
bookSchema.methods.fullTitle = function() {
    return [this.series.title, this.title].join(" - ");
}
但很明显,这是行不通的


如何执行此操作?

您需要先填充引用模型,然后才能访问其属性。使用现有设置,您可以执行以下类似操作:

bookSchema.methods.fullTitle = function() {
  this.populate('series', function(err, result) {
    return [result.series.title, result.title].join(" - ");
  });
}
更多详情请参见猫鼬种群。

您需要先填充引用模型,然后才能访问其属性。使用现有设置,您可以执行以下类似操作:

bookSchema.methods.fullTitle = function() {
  this.populate('series', function(err, result) {
    return [result.series.title, result.title].join(" - ");
  });
}
更多详情请参见猫鼬种群。

噢,哇,我不知道你可以在
上调用
填充
。谢谢哦,哇,没想到你可以在
上调用
填充
。谢谢