hi, how i can access public attribute in executeSql block
i have this code (this.orders as defined as “public orders: any;”)
public getOrders()
{
this.dataBase.executeSql('select * from orders',{})
.then(function(result)
{
this.orders = [];
if(result.rows.length > 0)
{
for(var i =0; i< result.rows.length;i++)
{
this.orders.push({code_id: result.rows.item(i).order_id, code: result.rows.item(i).code});
}
}
})
.catch(e => console.log(e));
}
and when try use this.orders i get “TypeError: Cannot set property ‘orders’ of undefined”"
You pretty much never want to type function
in an Ionic app.
Instead, utilize the fat arrows syntax () =>
.
So replace, function(result)
with:
(result) =>
1 Like
tkx man… solved…
what the reason for use this sintax?
this
is a weird concept in JavaScript.
For those that come from a class-based OOP background we expect this
to represent the current instance of whatever class we’re in, right? In JS however, this
is whatever the “calling context” is. Which could be any number of things, but is usually not the instance of our class. It could be the window document, the sqlite javascript object, etc…
The way around that is to do something like this:
var self = this;
someFunction(function (result) {
self.message = result;
});
However, they decided with TypeScript that they’d just introduce new syntax, the “fat arrows”!
Which does the same thing, but does it behind the scenes for you. Plus it saves you from typing “function” a bunch of times.
1 Like