javascript - Angular http client observable pipe map - Stack Overflow

I have this in my servicedoSomething(): Observable<any> {return this.http.get('').pipe(

I have this in my service

  doSomething(): Observable<any> {
    return this.http.get('')
      .pipe(
        map((data: Response) => {
            if (data && data['success'] && data['success'] === true) {
              return true;
            } else {
              return false;
            }
          }
        )
      );  
  }  

This works, I can subscribe to the function from my ponent, for example

    this.myService.doSomething().subscribe(
        (result) => {
            console.log(result);
        },
        (err) => {
            console.log("ERROR!!!");
        }
    );

Al this already works, but I want to refactor so that I can remove

 if (data && data['success'] && data['success'] === true)

in my map. So that the map function only will be executed when I have upfront did the check. My first thought was to add a function in the pipe stack that will take the Response from the http client, check if the the conditions are good, otherwise throw an error (with throwError function). But I'm struggling how to (well at least figure out which Rxjs function to use).

Can somebody help me out with this?

I have this in my service

  doSomething(): Observable<any> {
    return this.http.get('http://my.api./something')
      .pipe(
        map((data: Response) => {
            if (data && data['success'] && data['success'] === true) {
              return true;
            } else {
              return false;
            }
          }
        )
      );  
  }  

This works, I can subscribe to the function from my ponent, for example

    this.myService.doSomething().subscribe(
        (result) => {
            console.log(result);
        },
        (err) => {
            console.log("ERROR!!!");
        }
    );

Al this already works, but I want to refactor so that I can remove

 if (data && data['success'] && data['success'] === true)

in my map. So that the map function only will be executed when I have upfront did the check. My first thought was to add a function in the pipe stack that will take the Response from the http client, check if the the conditions are good, otherwise throw an error (with throwError function). But I'm struggling how to (well at least figure out which Rxjs function to use).

Can somebody help me out with this?

Share Improve this question edited Dec 6, 2022 at 0:15 Jason Aller 3,65228 gold badges41 silver badges39 bronze badges asked Oct 29, 2018 at 16:35 fransyozeffransyozef 5742 gold badges8 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 1

Try using this :

doSomething(): Observable<any> {

    return this.http.get('http://my.api./something')
      .pipe(
        mergeMap((data: Response) => {
            return of(data && data['success'] === true) 
          }
        ));  
}  

You have to perform the mergeMap first to do the check as your observable result will not be available if not...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744794590a4594128.html

相关推荐

  • javascript - Angular http client observable pipe map - Stack Overflow

    I have this in my servicedoSomething(): Observable<any> {return this.http.get('').pipe(

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信