I am trying to pass an array from service on to a page.
Service.ts
public getAmount() {
this.storage.query("SELECT amount FROM budget").then(
data => {
this.amount = [];
if (data.res.rows.length > 0) {
for (var i = 0; i < data.res.rows.length; i++) {
let item = data.res.rows.item(i);
this.amount.push(Number(new item.amount));
}
}
});
return this.amount;
}
on the page i simple call for the service method as so:
page.ts
data: this.Service.getAmount();
this should be simple but for some reason it doesnât work i get this error
EXCEPTION: Error: Uncaught (in promise): TypeError: item.amount is not a constructor
it appears that there are two problems first item.amount canât add value to an array
and second array cant be pushed on to the page
any ideas??
because youâre in an async call when you query your storage. so this.amount exist for the time the query is executed.
public getAmount() {
return this.storage.query("SELECT amount FROM budget");
}
then on the other side where you call your service this is where youâll go through your array returned and fill up your array to display it to the UI.
it did help a lot thanks ,
it did work to generate an array with the data from the db but the array is weird
placed in service.ts
public getAmount() {
return this.storage.query(âSELECT amount FROM budgetâ);
}
on page.ts
public passAvgWeek() {
let amount = new Array();
this.Service.getAvgWeek().then(
data => {
if (data.res.rows.length > 0) {
for (var i = 0; i < data.res.rows.length; i++) {
let item = data.res.rows.item(i);
amount.push(Number(item.mount));
}
}
});
//amount = [5,9,6,5,4,7,6,4,7];
return amount;
}
to test i just call it to consol log
console.log(this.passAvgWeek());
and get this weird array it shows empty brackets and the correct data below
but then when i uncomment this test line - amount = [5,9,6,5,4,7,6,4,7];
i get a proper array
Youâre trying to treat asynchronous code as if it were synchronous. Down that road lies nothing but pain. Donât expect passAvgWeek to return actual data. It canât and wonât, no matter what you try to do. It can return a Promise<Number[]> using the core of your then clause (which should be where amount is created and returned from, not outside of it). You cannot expect anything meaningful by logging what passAvgWeek itself returns, you need to do something like:
passAvgWeek(): Promise<Number[]> {
return this.Service.getAvgWeek().then((data) => {
let amount = [];
// build amount from data
return amount;
});
}
passAvgWeek().then((week) => {
// only in here can we access the actual "amount" array that was built in passAvgWeek()
});
much clearer thank you
but when i attempt to just build and
console log the the amount using the code
private passAvgWeek(): Promise<Number[]> {
return this.Service.getAvgWeek().then((data) => {
let amount = [];
if (data.res.rows.length > 0) {
for (var i = 0; i < data.res.rows.length; i++) {
let item = data.res.rows.item(i);
amount.push(Number(item.amount));
}
}
return amount;
});
}
it returns a bunch of syntax errors on this part, do i need to declare this "weekâ somewhere
line 52: passAvgWeek().then((week) => {
// only in here can we access the actual "amount" array that was built in passAvgWeek()
console.log(amount);
});
I donât know what the context is for your âline 52â, but a couple of things: since passAvgWeek is private, youâll probably have to be calling it as this.passAvgWeek. Also, when youâre inside the âthenâ clause, âweekâ is going to be the array you returned as âamountâ. It wonât be called âamountâ any more at that point.
Move passAvgWeek out of the page component and into the service (or modify getAvgWeek to include its logic). Declare a week property of type Number[] in the page component. Initialize it to an empty array immediately.
Then when you want to fill it, you do something like this in the page component:
I donât know when you want to do this: it could be called from someplace like ngOnInit or from a function invoked by a click event on a button or something. You canât just randomly put it in the body of the page component class, though, like you are apparently trying to do now.