0
 if(this.datashare.selectedtableId!=null) {
     console.log( "inside if condition");
     let resp= this.http.get(this.global.apiUrl+"columns/"+this.datashare.selectedtableId);
     resp.subscribe((data)=>this.users=data);
     console.log(this.users);
     this.count=this.users.length;
  }

when I console the users it shows all the data from API in my console like that (console.log(data)) But in my component, I try to get the data at the starting of the page by calling the service and assign it to a local variable it is giving undefined.

Ganesh
  • 5,808
  • 2
  • 21
  • 41
  • In order to make it clear, you should check the code (the first line is not formatted for example) and then include the code on which you're receiving the undefined value. – nilsandrey Nov 04 '20 at 07:11

1 Answers1

1

Assign the value for variable inside of the subscribe.

Because when ever you subscribe to an Observable current thread won't stop till the execution of it, simply it moves to the next line until it gets response from subscription.

Try to make below changes

...

resp.subscribe((data)=>{
    this.users = data;
    console.log(this.users);       //   --> you can find here response data 
    this.count = this.users.length;
});

console.log(this.users);           //  --> will give you undefined 
...

Happy Coding :)

Ganesh
  • 5,808
  • 2
  • 21
  • 41