Sqlite Ionic 3 - Problem saving SELECT responses to an array

Asked

Viewed 39 times

0

I’m on a project using Ionic 3 and need to manipulate data in sqlite. The current problem is the following, I run a SELECT in a table and store the return in an array (this.list_off) created in the class. When I use the console.log to show the value of this array INSIDE the database search function, it shows me the data I need, but when I try to use this array outside (after the SELECT that stores the data in it) the array is empty, as if numca had been filled. There is some different way to return and store a SELECT data in sqlite?

 getKeyList(){
   this.sqlite.create({
    name: 'rfleet.bd',
    location: 'default'
  }).then((db: SQLiteObject) => {
  db.executeSql('SELECT * FROM app_teclado WHERE list_id= 1', [])
    .then(res => {
      for(var i=0; i<res.rows.length; i++) {
        this.list_off.push({tec_id:res.rows.item(i).tec_id, tec_codigo:res.rows.item(i).tec_codigo,
          tec_data:res.rows.item(i).tec_data, tec_status:res.rows.item(i).tec_status,
          inst_id:res.rows.item(i).inst_id, list_id:res.rows.item(i).list_id, tipo:res.rows.item(i).tipo})
      } 

    }).catch(e => console.log("SELECT TECLADO ERROR", e) );
}).catch(e => console.log(e));

}

1 answer

0


I found a solution, I’m just not sure it’s the best way. I made the function work with answers in "promise", where the return of the function is a RESULT, type called with ajax.

getListId(){
return new Promise((resolve, reject)=>{
  this.sqlite.create({
    name: 'rfleet.bd',
    location: 'default'
  }).then((db: SQLiteObject) => {
    db.executeSql('SELECT * FROM app_listaoff WHERE list_status = 0', [])
      .then(res => {
          let id
          if(res.rows.length > 0){
            id = res.rows.item(0).list_id;
          }
          console.log("LAST list_id inserted", id);
          resolve(id)

      }).catch(e => console.log("SELECT app_listoff ERROR", e));
    }).catch(e => console.log(e));
}) 

Here is the call and the way I receive the response from the function.

this.getListId().then(res =>{
  this.list_id = res
})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.