The code is written in Angularjs in Ionic framework. I think people from Angular background can also answer this question.
When I call this function alert shows empty array "[]" and "undefined" respectively. I think this is happening cause of the asynchronous nature of JavaScript. I want this function to execute synchronously.
/*This is the constructor */
constructor(public navCtrl: NavController, public SP: SqliteProvider) {
}
/*my function*/
getCustomerById()
{
this.SP.getCustomerById(this.id);
this.customerById = this.SP.customerById;
alert(this.SP.customerById);
alert(this.SP.customerById[0]);
}
/* The function in SqliteProvider */
getCustomerById(cid)
{
this.customerById = [];
this.sqlite.create({
name: this.dbName,
location: 'default'
})
.then((db: SQLiteObject) =>{
db.executeSql('SELECT * FROM `customers` WHERE id= ?', [cid])
.then(result => {
var json = JSON.parse(result.rows.item(0).json);
//alert(json);
this.customerById.push(json);
// alert(JSON.stringify(this.customerObject));
})
.catch(e => console.log(e));
})
.catch(e => console.log(e));
}