setUsersData() and addNewUser() return Observable when finished its job.
Even tho this part of the code works fine, function from tap operator wont execute, same if I move the logic to subscribe. Any ideas why it happens ?
this.userDataService.setUsersData().pipe(
take(1),
switchMap(() => {
if (name !== undefined) {
console.log('New User created');
return this.userDataService.addNewUser(resData.email, resData.localId, name);
} else {
console.log('Name is undefined. Skipping user creation.');
return of();
}
}),
tap(() => {
console.log('data setted');
this.isLoading = false;
this.router.navigate(['/microblog']);
})
).subscribe();
I tried switching RxJs operators, but didnt get any better.
2
Answers
Both my function addNewUser() and case with name undefined was return of(), which doesnt really create an Observable as I learned just now. I chcanged the above to return of(void 0) and it works just fine.
this.userDataService.setUsersData().pipe(
take(1),
switchMap(() => {
if (name !== undefined) {
console.log(‘New User created’);
return this.userDataService.addNewUser(resData.email, resData.localId, name);
} else {
console.log(‘Name is undefined. Skipping user creation.’);
return of();
}
}),
tap(() => {
console.log(‘data setted’);
this.isLoading = false;
this.router.navigate([‘/microblog’]);
})
).subscribe();