1

Pourquoi je ne peux pas obtenir de résultat alors que j'essaie de remplir mon schéma (j'utilise mongoosejs). Dans mon cas, ma catégorie, sous-catégorie, schéma de sous-catégorie n'utilise pas _id. J'utilise un identifiant personnaliséImpossible de remplir à l'aide de mongoosejs

Voici mon schéma de produit:

..... 
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; 

Et ceci est mon contrôleur de produit:

'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); 
     }) 
} 

Merci beaucoup pour votre aide.

Répondre

1

Malheureusement, pour l'instant, vous pouvez seulement populate champs à l'étranger _id s. Remplir par différents domaines a été un heavily requested feature on the mongoose GitHub pendant un certain temps.

+0

Oh, merci beaucoup. Je vais essayer à nouveau en utilisant une autre méthode. –