Inside my model I have an array of objects. I need to insert objects to said array when creating a NEW document. I have found information on how to do it with findandupdate but I can’t find how to do it with save().
This is my model:
const PettyCashItemsSchema = Schema (
{
pettyCashId:{
type: Schema.Types.ObjectId,
ref:'PettyCash',
required: [true, 'La Caja Chica es Obligatoria']
},
item: {
type: Number,
unique: true
},
items:{
concept: {
type: String,
maxlength:50,
required: [true, 'El Concepto es obligatorio']
},
incomeAmount:{
type: Number,
maxlength:50,
default:0,
required: [true, 'El Ingreso es obligatorio']
},
expenseAmount:{
type: Number,
maxlength:50,
default:0,
required: [true, 'El Egreso es obligatorio']
},
description: {
type: String,
maxlength:50,
required: [true, 'La Observación es obligatoria']
},
status: {
type: Boolean,
default: true,
required: [true, 'El Estatus es obligatorio']
}
},
}
);
And I am trying this way but it never saves anything in the array of objects:
const pettyCashId= req.params.id;
const itemToPush = [{
concept: req.body.concept,
incomeAmount: req.body.incomeAmount,
description: req.body.description,
'createdBy':{
uid: req.uid,
username: req.user.username,
}
}];
const item = new PettyCashItems( { pettyCashId, $push: { 'items': itemToPush } } );
await item.save();
res.json ( item );
Thanks!