Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
Problem while populating orders and an array of products

I'm trying to create an e commerce web site using react, node and mongodb. I created the order schema which comports the cmdRef(string), theClient(Id of the user) and an array of Products(with ProductId and qty) like the code below:

Orderchema=new mongoose.Schema({
    CmdRef:String,
    Client:{type: mongoose.Schema.Types.ObjectId,
    ref: 'User'},
    TotalProducts:[{ProductId:{type: mongoose.Schema.Types.ObjectId,
    ref: 'Product'},Quantity:Number}],

and now I'm trying to create the route to get all the Orders

    CommandRouter.get('/AllCommands',async(req,res)=>{
  try {
    const cmd= await  Command.find({}).populate({path: 'Client', select: 'firstName lastName'}).populate({path:'TotalProducts'})
    // 
    res.json(cmd)
} catch(error)
    { 
 
        return res.status(500).send({message : "error get orders"})

    } 
})

Here I found a problem while populating the products Id in the table it populates the client Id and returns all information about the User but fail with the product table this is the full response using PostMan

Have any of you any Idea about how to populate the productId in the TotalProducts table?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Total products is not an ObjectId. You should be populating ProductId instead.

This should work.

const cmd = await Command
    .find({})
    .populate([
      {
        path: 'Client', 
        select: 'firstName lastName'
      },
      {
        path: 'TotalProducts'
        populate: 'ProductId'
      }
    ]);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda