Reputation: 35
I'm trying to retrieve data from a subscribed item but it doesn't change the currentMemberDetail: Member object, it remains undefined.
public currentMemberDetail: Member;
constructor(public authService: AuthService,
public memberService: MemberService,
private route: ActivatedRoute,
private router: Router) {
this.memberService.getOne(this.authService.currentUser)
.subscribe(m => {this.currentMemberDetail = m; console.log(m)});
}
ngOnInit(): void {
this.memberService.getOne(this.authService.currentUser)
.subscribe(m => {this.currentMemberDetail = m; console.log(m)});
console.log(this.currentMemberDetail);
}
Here is what i get from console logs
Member {_id: "5a44bdb416ff3e115c0d732a", pseudo: "admin", password: "admin", profile: "I'm the administrator of the site!", birthdate: undefined, …}
m has the object but I can't parse it to currentMemberDetail and it remains undefined.
I've tried ngOnInit and inside the constructor.
Any solutions? Thanks a lot!
Upvotes: 1
Views: 87
Reputation: 18271
When you call
console.log(this.currentMemberDetail);
It is outside of the subscribe. Because .getOne
is asynchronous, the line above is getting logged before it has been set
If you change the subscribe to:
.subscribe(m => {this.currentMemberDetail = m; console.log(this.currentMemberDetail)});
You should see it has been set.
Upvotes: 1