2016-10-05 87 views
1

爲什麼我在嘗試填充我的方案時無法獲得結果(我使用mongoosejs)。在我的情況下,我的類別,子類別,子類別方案不使用_id。我使用自定義ID。無法使用mongoosejs填充

這裏是我的產品方案:

..... 
categoryId: { 
    type: String, 
    ref: 'Catgory', 
    required: true 
}, 
subcategoryId: { 
    type: String, 
    ref: 'Subcategory', 
    required: true 
}, 
subsubcategoryId: { 
    type: String, 
    ref: 'Subsubcategory', 
    required: true 
}); 

const Product = mongoose.model('Product', product); 
module.exports = Product; 

這是我的產品控制器:

'getOne': function(req, res, next) { 
    if (typeof req.params.id !== 'string') return res.send({ 
     'error-code': 3 
    }); 

    Product.findOne({ 
      _id: req.params.id 
     }) 
     .populate({ 
      path: 'category', 
      select: 'name' 
     }) 
     .populate({ 
      path: 'subcategory', 
      select: 'name' 
     }) 
     .populate({ 
      path: 'subsubcategory', 
      select: 'name' 
     }) 
     .exec(function(error, response) { 
      if (error) return handleError(error); 
      return res.send(response); 
     }) 
} 

非常感謝您的幫助。

回答