0

I have a problem with a async function: tx.executeSql(). I'm doing a for() but as tx.execute is async my var i doesn't work the way I expect.

the var i when I do the query is always = 3 (the last number of my this.state.datausuarios array) u can see that in the console. so this doesn't work so well...

I'm working with React Native but I use the common asyn/await function but I don't know how to implement in this case

insertarDatos = async () =>{
    console.log('COMIENZA insertarDatos');
     await db.transaction((tx) => {
       console.log('Comienza Transaction');
        for(i in this.state.dataUsuarios){
          const sql = `INSERT INTO usuarios VALUES (
            '${this.state.dataUsuarios[i].id}', 
            '${this.state.dataUsuarios[i].nombre}',
            '${this.state.dataUsuarios[i].usuario}',
            '${this.state.dataUsuarios[i].password}',
            '${this.state.dataUsuarios[i].grupo}',
            '${this.state.dataUsuarios[i].faenas}'
          )`;
          console.log('antes de excute i es: ' + i);
          tx.executeSql(sql, [], (tx, results) => {
           console.log('dentro de executesql i es: ' + i);
            if(results.rowsAffected > 0){
              console.log('¡Datos insertados en BD!');
            }else{
              console.log('¡No se puedieron insertar datos en la BD!');
            }(error) => {
            console.log(error.message);
            }
          });
        }
        console.log('termina transaccion');
    });

    console.log('TERMINA insertarDatos');

  }

My console exits:

TERMINO openBD
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:22 COMIENZA insertarDatos
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:84 Comienza Transaction
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:84 antes de excute i es: 0
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:84 antes de excute i es: 1
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:84 antes de excute i es: 2
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:84 antes de excute i es: 3
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:104 termina transaccion
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:108 TERMINA insertarDatos
dc0e3583-a57a-4389-8dbd-5201b5fa6f2c:92968 *TERMINA componentDidMount
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:87 dentro de executesql i es: 3
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:89 ¡Datos insertados en BD!
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:87 dentro de executesql i es: 3
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:89 ¡Datos insertados en BD!
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:87 dentro de executesql i es: 3
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:89 ¡Datos insertados en BD!
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:87 dentro de executesql i es: 3
C:\Users\josen\React-Projects\GeslubApp\src\Screen\Login.js:89 ¡Datos insertados en BD!
Braven
  • 484
  • 1
  • 6
  • 27
  • 1
    Possible duplicate of [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – Heretic Monkey May 19 '19 at 00:10
  • You may also be interested in [Using async/await with a forEach loop](https://stackoverflow.com/q/37576685/215552) – Heretic Monkey May 19 '19 at 00:11
  • maybe you have to use `let or var` before declaring the variable of the function... – Conan May 19 '19 at 00:12
  • Yeah thank u so much. I declared var i as a let: for(let i in this.state.dataUsuarios) – Braven May 19 '19 at 04:37

0 Answers0