I'm trying to insert a data series into a database SQLite3
from NodeJS
by means of a loop with INSERT
, however, I need to have the variable I use for the callback
function perform the for
loop. It occurred to me to make a bind with the function of callback
but then I lose other variables that are filled in once the SQL
statement is executed, which is the lastID
and the change. The part of the corresponding code is the following:
for(var i =0; i<listaCaidas.length;i++)
{
db.run("INSERT INTO Caidas (idHistoriaActual, Fecha, MomentoDia, Donde, MecanismoCaida, precisoValoracionMedica) VALUES (? ,? ,?, ?, ?, ?)", idHistoriaActual, listaCaidas[i].fecha, listaCaidas[i].momentoDia, listaCaidas[i].lugar, listaCaidas[i].mecanismoCaida, listaCaidas[i].valoracionmedica, function(err,rows){
if(err)
{
console.log("Surgió un error al tratar de insertar una caída \n"); res.status(500);
res.status(0);
}
else
{
var idCaida = this.lastID;
console.log("La caida que s eha insertado tiene id: " + this.lastID);
console.log("El valor de i es: " + this.i);
var listaSintomas = listaCaidas[this.i].listaSintomas;
console.log("Añadimos las consecuencias de la caída \n");
}
}.bind({ i : i}));
}
}
With the code above it returns the variable i
correctly, but this.lastID
appears as undefined
, without removing the .bind
the this.lastID
has value but the i
appears as undefined
.
I hope your help, greetings.