Why I can't get result while try to populating my scheme (I use mongoosejs). In my case my category, subcategory, subsubcategory scheme don't use _id. I use custom id.
Here's my product scheme:
.....
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;
And this is my product controller :
'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);
})
}
Thank you very much for your help.
Unfortunately, as of right now, you can only populate
fields using foreign _id
s. Populating by different fields has been a heavily requested feature on the mongoose GitHub for quite a while.