2013-07-03 22 views
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); 
     }) 

    }) 
    } 

我在哪裏錯了方法 '路徑'?

回答

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

subcategories.products是不明n路徑爲Category模式,因此您需要更改用於羣體的模型。以下兩種方法都可以工作:

// 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) { 
相關問題