Here is the code snippet:
let sqlLocalSelect = new SqlLocalSelect();
var variable = sqlLocalSelect;
console.log("variable " + JSON.stringify(variable));
This is the corresponding class:
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 data found");
}
console.log("Query executed successfully");
console.log("li_sitios "+ JSON.stringify(this.li_sitios));
}, (error) => {
console.log("Error executing query: " + JSON.stringify(error));
});
}, (error) => {
console.log("Error connecting to local database: " + JSON.stringify(error));
});
return this.li_sitios;
}
}
Explanation of the issue: The output displays empty first and then shows the actual query results in console.
Sample 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)
...
(Additional log messages and error details.)
...
06-07 18:23:51.290 21450-21450/io.ionic.starter I/chromium: [INFO:CONSOLE(60834)] "Query executed successfully", 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 [{...}, {...}, {...}]", source: file:///android_asset/www/build/main.js (60835)