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