Angular Observable don't set local variable while calling API

distance

I'm trying to set value in a local variable like this:

export class memberComponent implements OnInit {

   member : Member = new Member();
   constructor(private memberService: MemberService) {}

   ngOnInit() {
     this.loadMember();
     console.log("member 1: ", this.member); // in here member is empty
   }

   loadMember(){
     this.memberService.getMember("john").subscribe(mem => {
       this.member = mem;
       console.log("member 2: ", this.member);// in here member is NOT empty
     });
   }
}

And because the member is empty nothing shows in the page. Here is my component.html

<h1>{{member.userName}}</h1>

Finally, this is the memberService:

export class MemberService {

  baseUrl = environment.apiUrl;

  constructor(private http: HttpClient) {}

  getMember(userName: string) {
    return  this.http.get<Member>(this.baseUrl + 'users/' + userName);
  }

}

I tried to user pipe(take(1)) method and interface instead of class but there were no difference. And nothing has shown in my html page.

What's the problem and how can I fix it?

Aakash Garg

You should be receiving a can't read userName of undefined error in browser console. to fix this try use elvis operator like below :-

<h1>{{member?.userName}}</h1>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular constructor doesn't set a local variable before ngOnInit() is called

Get desired result while calling API using Postman but don't when using RestShart in console program

Can not assign observable data to local variable in angular

Subscribe a local variable Value to an Observable of variable in another Component in Angular

angular observable async/ngrxLet pipes don't subscribe

Observable Angular don't work how I expect

angular - using async pipe on observable<Object> and bind it to local variable in html

How to assign local variable from observable Angular2

Don't remove local file which is in .gitignore while change branch

Why is a variable global when set with `read variable` inside a `while` loop, but local when set with `while read variable`?

Set variable while navigating using angular

Angular don't print informations API

What exactly happens when i don't use "self" key while initialising a variable like m1 inside a class method. m1 is local,object or class variable?

Local variable referenced before assignment (don't want to use global)

unshift & splice don't work for observable arrays when calling from prototypes

Can't set a variable inside a while loop

Variable while calling a function

How to set a value from observable to a variable in Angular 2

Checking local variable while unit testing on a component in Angular gives error

web api support parameters while they don't have one

push to observable variable in angular

In PyQT why somes widgets needs the "self" parameter before calling them, while others don't

angular can't set value to public variable

Angular variable in set Timeout doesn't refresh?

Mocking local variable in calling method

How to pass a local variable to a callback while getting something returned by the calling function?

Set value of local iteration scoped variable in angular 2

mobx-react observer don't fires when I set observable

Angular: Observable service doesn't get data from api