2

I have to make a check in my resolver, if it's fine, I reassign the object, otherwise, I return an observable with the value false.

Here is my code:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    return this.callApi().subscribe((result) => {
        if (blabla === true) {
            return Observable.of(result);
        }
        this.router.navigate([ '/' ]);
        return Observable.of(false);
    });
}

The Observable.of(false) seems to work, I go to the homepage, but with Observable.of(result) , I don't get the result in my component ...

Thank you for your help

5
  • You shouldn't return anything in .subscribe(). What's this supposed to do btw? Commented Oct 26, 2016 at 13:34
  • I'm checking if the object has been disabled, in this case I go to the homepage with a message. Commented Oct 26, 2016 at 13:36
  • So why are you returning an Observable in .subscribe()? Commented Oct 26, 2016 at 13:37
  • Because I need to check in the resolver. Commented Oct 26, 2016 at 13:54
  • 1
    Finally I fount the solution, switchMap instead of subscribe Commented Oct 26, 2016 at 13:54

1 Answer 1

1

You don't even need switchMap, you have the result available. Note that you convert both result and false to observables, but you can return them directly.

return this.callApi().map(result => (blahblah ? result : false));

The side effect inside the caller is not a best practice, you can handle it outside:

resolve(...).filter(result => !result).subscribe(() => this.router.navigate([ '/' ]));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.