'Angular observable and subscription
i have one observable function in my service which i have subscribe but i am getting [object Object] instead of token value
service code:
get bearerToken(): Observable<string | null> {
return this.store.select(AuthStatusState.getToken).pipe(
map((token) => {
if (token) {
return token;
} else {
return null;
}
}),
take(1)
);
}
code to which i have subscribed.
const authToken = this.authService.bearerToken.subscribe();
console.log(authToken);
i want token to be saved in const instead of object
Solution 1:[1]
Like this authToken
is a subscription.
const authToken = this.authService.bearerToken.subscribe();
Like this authToken
will be the value emitted by bearerToken
.
let authToken;
this.authService.bearerToken.subscribe( data => authToken = data);
console.log(authToken);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |