I need help. I am setting up a system for loading notes per subject per student.
I put together a spreadsheet to load these notes (angular) and once they finished loading, I sent all that object to be processed, but when I try to save those notes (I search the base, if there is a modification, if not, I believe it) , it generates all the equal records.
The code is this:
var query = {};
query.delegacion = data.delegacion;
query.disciplina = data.disciplina;
query.periodo = data.periodo;
query.nivel = data.nivel;
query.anio = data.anio;
for (var alumno in data.notas) {
var id_alumno = alumno;
for (var materia in data.notas[alumno]) {
var id_materia = materia;
var nro_nota = data.notas[alumno][materia];
query.alumno = id_alumno;
query.materia = id_materia;
Nota.find(query)
.exec(function(err, result) {
if (err) res.send(err);
if (result.data) {
result.nota = nro_nota;
result.save(function(err, doc) { });
} else {
var notas = new Nota();
notas.delegacion = data.delegacion;
notas.disciplina = data.disciplina;
notas.periodo = data.periodo;
notas.nivel = data.nivel;
notas.anio = data.anio;
notas.alumno = id_alumno;
notas.materia = id_materia;
notas.fecha = req.body.fecha;
notas.nota = nro_nota;
notas.recupera = false;
notas.save(function(err, doc) { });
}
});
}
}
This is the object I receive in NodeJS:
{
delegacion: '57e3108ee0a5427810ed1dd1',
disciplina: '589888d6dfae11da7c38c22e',
periodo: '58a67d7798f945ea6928b055',
nivel: '58a67278038397c566181428',
anio: 2017,
notas: {
'584878b97d60ab072930fba9': {
'58ae61d38f89a5574c28bdd4': 8.5
},
'57e4393fcc6f162d3166925e': {
'58a71819fc82f69b0e1cf70b': 10,
'58ae61d38f89a5574c28bdd4': 7.5
}
}
}
I understand that it can be handled with promises, but I did not realize very well how to implement them.
Of course, I thank you for any help you can give me.