Hi guys,
I need your help in fitting an insert of arrays of N Quantity to a field, from my controller.
Let me explain, I would like to be able to push to a field N record amount of an Array, I have tried to modify my schema with references of this type
field: [SchemaReference]
but since it is in different schemas I cannot, and what I want to do is to be able to save as much user mails as possible in a group, so that all of them can have a view of the message. this is my controller
saveGroup : function (req, res) {
//recoger parametros por post
var params= req.body;
//validar datos
try{
var validate_nameChat = !validator.isEmpty(params.nameChat);
var validate_email = !validator.isEmpty(params.email);
}catch(err){
return res.status(404).send({
message:'faltan datos por enviar // Save',
});
}
if(validate_nameChat && validate_email){
//crear objeto a guardar
var group = new Group();
//asignar valores
group.nameChat = params.nameChat;
group.user = req.user.sub;
group.email = params.email;
//group.urlFile=null;
var contacts = {
user: req.user.sub,
email: req.body.email,
};
group.email.push(contacts);
//guardar topic
group.save((err, groupStored) =>{
//devolver una respuesta
console.log(groupStored);
//console.log(groupStored);
if (err || !groupStored) {
return res.status(404).send({
status:'error',
message:'Ya el tienes un chat con el usuario que quieres agregar'
});
}
return res.status(200).send({
status:'succes',
groupStored
});
});
}else{
return res.status(404).send({
message:'Los datos no son Validos'
});
}
},
the schema is as follows
var Schema = mongoose.Schema;
var MessageSchema = new Schema({
messageContent:{ type:String },
//idStatusMessage:{ type:Boolean },
user :{ type: Schema.ObjectId, ref:'User'},
urlFile:{ type:String, default:'Image.png'},
},{ versionKey:false,
timestamps:true,
});
var Message = mongoose.model('Message' ,MessageSchema);
var GroupsSchema = new Schema({
nameChat:{ type:String },
user :{type: Schema.Types.ObjectId, ref:'User'},
//contact:[ContactSchema]
email:[ { type:String, unique:true, trim:true, require:true}],
messages:[MessageSchema]
},{ versionKey:false,
timestamps:true,
});
//cargar grupos
GroupsSchema.plugin(mongoosePaginate);
module.exports = mongoose.model('Group', GroupsSchema);
I found this kind of documentation {$ push: {field: {$ each: [element1, element2, …, elementN}}}
that could help me but I can’t find a way to apply it to be able to add different emails in the group