I work on angular 7 I face issue count rows variable give me undefined value
although returned data is 3
Expected result I need is Count rows must equal 3
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 although returned data value is 3
data returned on count items function
[
{
"countItems": 3
}
]
What I have tried:
console.log("final data" + data[0].countItems)
give me on console as
final data 3
Top comments (2)
I dont know react, but it looks like you do the following now.
Since you console log the data[0].countItems we know that that variable is not the issue. That leaves the possibility that your subscriber callback might never be called, or be so later than you expect
The method countitems() that you defined, does not actually assign a value to this.Countrows, what it does is setup a subscriver and a callback that when, and if, the callback is called, it will set the value this.Countrows to whatever data[0].... brings
Try and figure out when/why not the callback is called
thanks for reply
can you show me function will be to work