Angular 2:两个后端服务调用第一个服务成功 [英] Angular 2: Two backend service calls on success of first service

查看:19
本文介绍了Angular 2:两个后端服务调用第一个服务成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Angular 2 应用程序中,我有如下后端服务.

In my Angular 2 app I have backend service as below.

getUserInterests() {
    return this.http.get('http://localhost:8080/test/selections').map((res: Response) => res.json());
}

调用此服务后,我想在前一个服务成功后调用另一个服务.

After calling this service I want to call another service on success of previous one.

二次服务

let params: URLSearchParams = new URLSearchParams();
    params.set('access_token', localStorage.getItem('access_token'));
    return this.http.get('http://localhost:8080/user/selections', { search: params }).map((res: Response) => res.json());

这两个服务分别返回两个 JSON 数组.然后我需要用这两个数组登录.

These two services separately return two JSON Arrays. Then I need to do some login with these two arrays.

已编辑

service.ts

getUserInterests() {
    return this.http.get('http://localhost:8080/test/selections').map((res: Response) => res.json());
}

getSavedSelections() {
    let params: URLSearchParams = new URLSearchParams();
    params.set('access_token', localStorage.getItem('access_token'));
    return this.http.get('http://localhost:8080/interest/user/selections', { search: params }).map((res: Response) => res.json());
}

getSelectionList() {
    var obs = this.getUserInterests().flatMap(
        (interests) => {
            return Observable.forkJoin([
                Observable.of(interests),
                this.getSavedSelections()
            ]);
        }
    );
    return obs;
}

然后我在我的其他 ts 文件中使用以下来调用服务.

Then I am using following in my other ts file to call the service.

export class InterestsComponent {
  private interests;
  private saved_interests;
  constructor(private dataService: DataService) {
    this.dataService.getSelectionList().subscribe(
        (result) => {
            var interests = result[0];
            var selections = result[1];
        }
    );
  }
}

但这会在控制台日志中出现以下错误.

But this give following error on console log.

ORIGINAL EXCEPTION: TypeError: this.dataService.getSelectionList is not a function

感谢任何建议.

推荐答案

您需要利用 flatMap 运算符在前一个请求完成后调用一个请求:

You need to leverage the flatMap operator to call one request after the previous one completed:

this.service.getUserInterests().flatMap(
  (interests) => {
    let params: URLSearchParams = new URLSearchParams();
    params.set('access_token', localStorage.getItem('access_token'));
    return this.http.get('http://localhost:8080/user/selections', {
      search: params
    }).map((res: Response) => res.json());
  }
);

订阅此数据流时,您只会收到上次请求的结果.

When subscribing to this data flow, you will only receive the result of the last request.

您可以使用 Observable.forkJoin 方法返回两者.这是一个示例:

You could use the Observable.forkJoin method to return both. Here is a sample:

var obs = this.service.getUserInterests().flatMap(
  (interests) => {
    return Observable.forkJoin([
      Observable.of(interests),
      this.service.getUserSelections()
    ]);
  }
);

obs.subscribe(
  (result) => {
    var interests = result[0];
    var selections = result[1];
  }
);

这篇关于Angular 2:两个后端服务调用第一个服务成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆