I work on angular 7 I face issue countrows variable give me undefined value
although I assigned it with value as below
Expected result I need is to get value 3 from returned array of object
public Countrows:Number;
ngOnInit(): void {
countitems()
console.log("final count rows is" + this.Countrows);
}
countitems()
{
this._inventory.GetcountItems().subscribe(
data =>this.Countrows=data[0].countItems
)
}
it give me final data undefined
data returned on variable Countrows
[
{
"countItems": 3
}
]
What I have tried:
console.log("final data" + data[0].countItems)
give me on console as
final data 3
Satya KarkiPosted Jul 11, 2021, 5:21 PM
Hi,
Typescript/Javascript functions are of asyncronous behaviour so it does not wait for first function to complete and give undefined from next function.
If you check console output then you will get as:
final count rows is undefined // first
Actual 3 // second
This is because your this funtion console.log("final count rows is" + this.Countrows); will not wait for this countitems().
This is why they are asyncronous.