I have a problem in the Historial array (specifically in the Categoria field of each document, the rest of the fields are sent correctly). Although in the model I clarify that it is an ObjectId and in the database it is also shown as an ObjectId, I am receiving in the json only the id of the category (as if it were a string), I mean, I am not linking well with Categoria that is in another collection (this only happens inside the array, the Categoria field of the parent document works perfectly). Maybe it’s a syntax problem in my controller as I never work sending arrays with objects.
Controller:
crear: async (req, res, next)=> {
console.log(req.body);
const producto = new productosModel({
nombre:req.body.nombre,
precio:req.body.precio,
stock:req.body.stock,
categoria:req.body.categoria,
fechaCreac:req.body.fechaCreac,
historial: {
precio:req.body.precio,
stock:req.body.stock,
categoria:req.body.categoria
}
})
const document = await producto.save();
res.json({document, mensaje: "Creado con éxito", estado: "Ok"});
} catch(e) {
console.log(e);
res.status(200).json({mensaje:"Ese producto ya existe", estado: "Error"})
}}
Model: