简体   繁体   中英

Observable function not getting called

I need to execute getData() and then proceed with another call getOtherData But I have an issue of my getData() not getting executed. Also I need to return data from this.service.getOtherData() so I can't use .subscribe() Here is how I composed my operators.

My first call I need to make:

public getData(): Observable<string> {
    return this.http.get<string>(apiUrl)
}

Does not work:

myFunction() {
    this.service.getData().pipe(
        tap(data => {
            doStuff(data); // not getting here
        }),
        switchMap(() => {
            return this.service.getOtherData().pipe(
                tap((data) => {
                    sc.dispatch(new DoSomethingElse(data));
                }),
                catchError(err => of(tap(() => {})))
            );
        })
    );
}

Any thoughts how to fix?

You need to call .subscribe inorder to get them called,

this.dataService.getData()
      .pipe(
        tap(console.log),
        tap(data => doStuff(data)))
      ).subscribe();

Try this:

public getData(): Observable<string> {
    return this.http.get<string>(apiUrl).subscribe();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM