rxjs TypeError: You provided 'undefined' where a stream was expected

212 Views Asked by At

I am facing an error triggered after subscribing to an Observable like so.

ngOnInit() {
    const defaultTitle = 'Something';
    this.router.events
      .pipe(
        filter((event) => event instanceof NavigationEnd),
        map(() => {
          const child = this.route.firstChild;
          if (child?.snapshot?.data.title) {
            return `${child.snapshot.data.title} | ${defaultTitle}`;
          }
          // fallback
          return defaultTitle;
        }),
        switchMap(title =>  {
          this.titleService.setTitle(title);
          return this.initialiseOffers() // returns Observable<{data: Offers;}>
        })
      ).subscribe((state) => {
        this.handleRedirect(state.data)
      });
      
}

handleRedirect(offers: Offers) {
  this.router.navigate(['offers'], {
          replaceUrl: true,
        });
  }
}

Error thrown is

ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.

This is coming from the router.navigate invocation when I examine my stack trace, but I cant figure out what's the actual cause of my error.

I am using switchMap to switch to another observable, but I dont need the result of the outer observable, only that I need to wait for that outer one to complete. I'm wondering if this is possibly a cause of my issue?

Can someone kindly point me in the right direction here please - and yes I am a bit of newbie with rxjs, so please be kind :D

Thanks

0

There are 0 best solutions below