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

77
Views
Can't Add One Schema To Another

I have two schema as User and Warehouse. And my user schema

const UserSchema = new Schema({  
    name: {
        type: String,
        required : [true, "Please provide a name"]
    },
    warehouses : [
        {
            
            type: Schema.Types.ObjectId,
            ref: "Warehouse"
            
        }
    ]
});
module.exports = mongoose.model("User",UserSchema);

and a warehouse schema

const WarehouseSchema = new Schema({    
    name: {
        type: String,
        required: [true, "Please provide a name"]
    }
});

and when use request to user/:id/warehouse a put request I want to add the warehouse to user's warehouses array.

const addWarehouse = asyncErrorWrapper(async(req, res, next) => {
    // const {id} = req.params;
    
    const user = req.data;
    if(Object.keys(req.body).length===0){
        return next(new CustomError("There is no warehouse info",400));
    }
    const {name} = req.body;

    const warehouse = await Warehouse.create({
        name
        

    });
    user.warehouses.push(warehouse);
    user.save();

    return res.status(200)
        .json({
            success:true,
            data: user.warehouses
        }); 
});

enter image description here

and I request for the second, it's not adding the new one and just show me the only warehouse that I requested. And if i request for user's warehouses, i get empty array. And the warning that i got is

enter image description here

about 4 years ago · Juan Pablo Isaza
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!