this.lastID returns null when I make a bind in the callback of a db.run () - NodeJS

0

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.

    
asked by Brank Victoria 29.11.2016 в 13:48
source

1 answer

1

I can think of this solution using an immediately invoked function:

for (var i = 0; i < listaCaidas.length; i++) {
(function (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: " + i);
                var listaSintomas = listaCaidas[i].listaSintomas;
                console.log("Añadimos las consecuencias de la caída \n");

            }
        })
})(i);
}

Of course this is equivalent to defining the content of the loop as a function and calling it with i as a parameter.

This solves the reason why you were using this.i instead of i

    
answered by 02.12.2016 в 01:03