Reputation: 131
my service function is ..
getPro():any{
this.database.all("SELECT * FROM product").then(rows => {
console.log("hello pro hear....")
let proList:Product[]=[]
for(var row in rows) {
proList.push(new Product(rows[row][0],rows[row][1],rows[row][2],rows[row][3],rows[row][4]))
/* proList.push({
"id": rows[row][0],
"name": rows[row][1],
"hsn": rows[row][2],
"rate": rows[row][3],
"tax":rows[row][4]
})*/
console.log("list ",rows[row][0])
}
console.log("total pro ",proList.length)
return "hello"
}, error => {
console.log("SELECT ERROR", error);
});
}
and my component is ..
proList:any
fatchPro(){
this.proList= this.dataservice.getPro();
console.log("fatchPro" ,this.proList)
}
but when i console.log(proList) it will get undefined
how can i solve this error...
Upvotes: 0
Views: 69
Reputation: 34445
Try that
getPro():any{
return this.database.all("SELECT * FROM product").then(rows => {
console.log("hello pro hear....")
let proList:Product[]=[]
for(var row in rows) {
proList.push(new Product(rows[row][0],rows[row][1],rows[row][2],rows[row][3],rows[row][4]))
/* proList.push({
"id": rows[row][0],
"name": rows[row][1],
"hsn": rows[row][2],
"rate": rows[row][3],
"tax":rows[row][4]
})*/
console.log("list ",rows[row][0])
}
console.log("total pro ",proList.length)
return "hello"
}, error => {
console.log("SELECT ERROR", error);
});
}
fatchPro(){
this.proList= this.dataservice.getPro().then(list =>
{
this.proList = list;
console.log("fatchPro" ,this.proList)
});
}
Upvotes: 1
Reputation: 2098
Your dataservice.getPro function
is creating a Promise and subscribing to it, but not returning anything itself. Therefore the proList
is not set. You can return the Promise and subscribe to it in your fatchPro function
:
getPro(): Promise<any>{
return this.database.all("SELECT * FROM product").then(rows => {
return rows.map(row => new Product(row[0], row[1], row[2], row[3], row[4]));
}, error => {
console.log("SELECT ERROR", error);
});
}
-
proList: Product[];
fatchPro() {
this.dataservice.getPro().then(proList => {
this.proList = proList;
console.log("fatchPro" ,this.proList);
});
}
Upvotes: 0