1

我的类别架构是:

var CategorySchema = new Schema({
  subcategories:[{type : Schema.ObjectId, ref : 'Category', null: true}],
  parent: {type : Schema.ObjectId, ref : 'Category', null: true},
  products:[{type : Schema.ObjectId, ref : 'Product'}]
})

当我尝试像下面那样进行嵌套填充时,我得到“无法调用未定义的方法'路径''

list: function (options, cb) {
    var criteria = options.criteria || {}
      , Category = this

    this.find(criteria)
      .populate('subcategories')
      .exec(function(err,docs){
        if(err){
          console.log(err.message);
          cb(err);
        }
        var opts = [
              { path: 'subcategories.products' }
        ];
        Category.populate(docs, opts, function(err, docs2) {
          if(err){
                cb(err);
          }          
          cb(docs2);
        })

     })
  }

我哪里错了?

4

1 回答 1

1
var opts = [
  { path: 'subcategories.products' }
];
Category.populate(docs, opts, function(err, docs2) {

subcategories.productsCategory模式的未知路径,因此您需要更改用于填充的模型。以下两种方法中的任何一种都可以:

// 1
var opts = [
  { path: 'subcategories.products' }
];
Product.populate(docs, opts, function(err, docs2) {

或者

// 2
var opts = [
  { path: 'subcategories.products', model: 'Product' }
];
Category.populate(docs, opts, function(err, docs2) {
于 2013-08-26T23:11:51.280 回答