0

This is my call

let sqlLocalSelect = new SqlLocalSelect();
var variable = sqlLocalSelect;
console.log("variable " + JSON.stringify(variable));

this is the class i call

import { SQLite } from 'ionic-native';

export class SqlLocalSelect {

database: SQLite;
private li_sitios: Array<Object> = [];

constructor() {}

get get_li_sitios(): Array<Object> {
this.database = new SQLite();
this.database.openDatabase({name: "data.db", location: "default"}).then(() => {
  this.database.executeSql('SELECT * FROM Markers;', [])
  .then((data) => {
    if(data.rows.length > 0){
      this.li_sitios = [];
      for(var i = 0; i < data.rows.length; i++) {
        var value:string = "no";
        if (data.rows.item(i).synchronised) {
          value = "si";
        } else {
          value = "no";
        }
        this.li_sitios.unshift({
          id: data.rows.item(i).id, 
          id_externa: data.rows.item(i).id_externa,
          id_owner: data.rows.item(i).id_owner, 
          name: data.rows.item(i).name, 
          lat: data.rows.item(i).lat, 
          lng: data.rows.item(i).lng,
          draggable: false, 
          synchronised: value,
          fecha: data.rows.item(i).fecha,
          hora: data.rows.item(i).hora
        });
      }
    } else {
      console.log("No se encontraron datos");
    }
    console.log("Consulta realizada correctamente");
    console.log("li_sitios "+ JSON.stringify(this.li_sitios));

  }, (error) => {
    console.log("Error ejecutando la consulta: " + JSON.stringify(error));
  });
  }, (error) => {
    console.log("Error al conectar con la base de datos local: " + JSON.stringify(error));
  });
  return this.li_sitios;
  }
}

Basically it is a query to an sqlite database, but the data output does not run in console in the correct order

This is my output

06-07 18:23:51.030 21450-21450/io.ionic.starter I/chromium: [INFO:CONSOLE(60258)] "variable []", source: file:///android_asset/www/build/main.js (60258) 06-07 18:23:51.120 21450-21450/io.ionic.starter I/chromium: [INFO:CONSOLE(179)] "OPEN database: data.db - OK", source: file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js (179) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: java.sql.SQLException: sqlite3_step failure: cannot rollback - no transaction is active 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.liteglue.SQLiteGlueConnection$SQLGStatement.step(SQLiteGlueConnection.java:135) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLiteConnectorDatabase.executeSQLiteStatement(SQLiteConnectorDatabase.java:214) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLiteConnectorDatabase.executeSqlBatch(SQLiteConnectorDatabase.java:114) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLitePlugin$DBRunner.run(SQLitePlugin.java:340) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.lang.Thread.run(Thread.java:831) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: java.sql.SQLException: sqlite3_step failure: cannot rollback - no transaction is active 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.liteglue.SQLiteGlueConnection$SQLGStatement.step(SQLiteGlueConnection.java:135) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLiteConnectorDatabase.executeSQLiteStatement(SQLiteConnectorDatabase.java:214) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLiteConnectorDatabase.executeSqlBatch(SQLiteConnectorDatabase.java:114) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLitePlugin$DBRunner.run(SQLitePlugin.java:340) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.lang.Thread.run(Thread.java:831) 06-07 18:23:51.290 21450-21450/io.ionic.starter I/chromium: [INFO:CONSOLE(60834)] "Consulta realizada correctamente", source: file:///android_asset/www/build/main.js (60834) 06-07 18:23:51.290 21450-21450/io.ionic.starter I/chromium: [INFO:CONSOLE(60835)] "li_sitios [{"id":4,"id_externa":0,"id_owner":1,"name":"Nombre 4","lat":22.834722222222,"lng":-103.91194444444,"draggable":false,"synchronised":"si","fecha":"2017-6-5","hora":"15:36"},{"id":3,"id_externa":0,"id_owner":1,"name":"Nombre 2","lat":22.534722222222,"lng":-103.91194444444,"draggable":false,"synchronised":"si","fecha":"2017-6-5","hora":"15:29"},{"id":2,"id_externa":15,"id_owner":1,"name":"Nombre 2","lat":22.534722222222,"lng":-103.91194444444,"draggable":false,"synchronised":"no","fecha":"2017-6-5","hora":"15:24"},{"id":1,"id_externa":16,"id_owner":1,"name":"Nombre 1","lat":22.034722222222,"lng":-103.91194444444,"draggable":false,"synchronised":"no","fecha":"2017-6-5","hora":"15:22"}]", source: file:///android_asset/www/build/main.js (60835)

As I mention it first it appears the result of the get empty and then the results of the query is throwing to console.

Juan Lozoya
  • 209
  • 6
  • 13
  • 1
    Your database query is async (you're using a promise) it's executing the call to the database and then returns the empty array (without waiting for the async call to resolve). – cyr_x Jun 08 '17 at 00:05
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Igor Jun 08 '17 at 00:41

2 Answers2

2

As I mention it first it appears the result of the get empty and then the results of the query is throwing to console.

This is the nature of async programming. Very similar to a simplified example:

const arr = [];
setTimeout(()=>{
   arr.push(1);
   console.log(arr); // [1]
}, 1000);
console.log(arr); // []

Code order by line number is not necessarily the execution order. This sounds simple (and it really is) but we've seen lots of beginners missing it so worth mentioning.

basarat
  • 261,912
  • 58
  • 460
  • 511
0

Thank you very much, I resolved it with your help

  this.sqlLocalSelect = new SqlLocalSelect();
  setTimeout(()=>{
    var variable = this.sqlLocalSelect.get_li_sitios;
    console.log("variable " + JSON.stringify(variable));
  }, 1000);
Juan Lozoya
  • 209
  • 6
  • 13