3
I have two degrees: courses and participants. an attendee when enrolling in a course, when I give a GET in any course, needs to return the enrolled attendees(participants) and when giving a GET to any participant, need to return the courses enrolled (cursesInscript).
When I give a POST or PUT to the participant and/or course, the information is not saved in the fields 'participants' or 'cursosInscritos', it returns with status 200 but not saved in the database
How can I do?
cursosSchema:
const cursoSchema = new mongoose.Schema ({
nomeCurso: {type: String, required : true },
participantes: [{type: mongoose.Schema.Types.ObjectId, ref: 'participanteSchema', required : false}]
})
participationsSchema
const participanteSchema = new mongoose.Schema ({
nomeCompleto: { type: String, required : true},
cursosInscritos: [{type: mongoose.Schema.Types.ObjectId, ref: 'cursoSchema'}]
})
controller
exports.inscricaoCurso = (req, res) => {
const {nomeCurso, local, periodo} = req.body
const inscreverCurso = new Cursos({
nomeCurso, local, periodo, participantes: req.params.participanteId,
})
inscreverCurso.save()
try {
Participantes.findOneAndUpdate({_id: req.params.participanteId}, {$push: {cursosInscritos: inscreverCurso._id}})
res.status(200).send({ mensagem: "Inserido com sucesso" })
} catch (error) {
return res.status(500).send({ mensagem: Error })
}
}