I'm working on a user service that keeps track of users by their user_id. It first checks if user_id is present in their cookies, if it isn't it makes a get request to the api to create a new one and returns the id in the response. My problem is that the user_id is being consumed before the response is completed. I have two questions:
- The BehaviourSubject is being consumed before it's even defined as a new BehaviourService, components that consume it are calling .subscribe() on
undefinedas a result and the app is crashing. - Is the really necessary for me to subscribe to the user_id every single time I want to retrieve it? The code is turning into hell to work with since i'm subscribing to the behavioursubject first and then inside the subscribe method I'm writing my code. I can't figure out a better way to go about it, and from the texts I've read they mention that linking streams is the right approach, but this just feels very wrong.
Here's a simplified example of what I'm doing
get ruid or create one - user.service.ts
constructor(...) {
public ruid: BehaviorSubject<any>;
if(!Cookies.get('ruid')) {
this.http.get(url).subscribe(
(value) => {
this.ruid = new BehaviorSubject(value)
Cookies.set('ruid', value)
}
)
} else {
this.ruid = new BehaviorSubject(Cookie.get('ruid'));
}
}
use ruid in component
constructor(private userService: UserService) {
data;
this.userService.ruid.subscribe(
(value) => {
this.data = this.http.get(url + value).map(res => res.json())
}
);
}