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

67
Vistas
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
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