Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda