Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!