1

I have this situation:

 this._userService
            .create(user)
            .pipe(
                switchMap(res => {
                    if (this.accountStore.accounts.length > 0) {
                        return this._accountsService.manageAccounts(
                            accounts,
                            res.idUser
                        );
                    } else return of();
                })
            )
            .subscribe(
                _ => {
                    this._router.navigate(["/app/main/user/"]);
                },
                () => (this.saving = false)
            );

if the accounts are higher then 0, it works ok. But if it goes to the else block, the return of() doesn't execute the next() to navigate to the user route;

What am I doing wrong?

kinny94
  • 376
  • 1
  • 5
  • 22
gog
  • 11,788
  • 23
  • 67
  • 129

3 Answers3

1

You need to pass a value into of to execute next operator or subscriber. In this case I just pass null as of(null)

Bunyamin Coskuner
  • 8,719
  • 1
  • 28
  • 48
0

Similar to this answer but different because of versions, you need to use the of({}).pipe(take(1)) to create an empty object observable and call next.

For your code:

...
} else return of({}).pipe(take(1));
...
0

Is cleaner if you use the creational operator EMPTY

The documentation says:

Observable that immediately completes.

Creates an Observable that emits no items to the Observer and immediately emits a complete notification.


Example:

// RxJS v6+
import { EMPTY } from 'rxjs';

//output: 'Complete!'
const subscribe = EMPTY.subscribe({
  next: () => console.log('Next'),
  complete: () => console.log('Complete!')
});
stackblitz
Community
  • 1
  • 1
Diego Juliao
  • 414
  • 6
  • 16