DEV Community

loading...

Value of count rows variable give me undefined although I assign it?

ahmedsaba
・1 min read

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

Discussion (2)

Collapse
pierrewahlberg profile image
Pierre Vahlberg

I dont know react, but it looks like you do the following now.

  • on init, call the method to get count
  • in get count method, you setup a subscriber with a callback
  • if the callback is called, you would set the calue of Countrows to the length of whatever data[0].countItems returns.

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

Collapse
ahmedsaba profile image
ahmedsaba Author

thanks for reply
can you show me function will be to work