简体   繁体   English

flatMap在Angular2中不起作用

[英]flatMap is not working in Angular2

Hello guys i just created a simple ajax call in which the response of first ajax call is used in second ajax call so i used flatmap in angular 2 to run first ajax call and use its response in second ajax call here is the code but only the first ajax call is working properly it seems like the code after .flatmap isnt running. 大家好,我刚刚创建了一个简单的ajax调用,其中在第二个ajax调用中使用了第一个ajax调用的响应,因此我在angular 2中使用了flatmap来运行第一个ajax调用,并在第二个ajax调用中使用了其响应,这里是代码,但是第一次ajax调用工作正常,似乎.flatmap未运行后的代码。 please help me to solve this issue. 请帮助我解决此问题。

 createPerson(personClass: User, addressClass: Address): Rx.Observable <string> { let headers = new Headers(); headers.append('Content-Type', 'application/json'); headers.append("Cache-Control", "no-cache"); headers.append("Cache-Control", "no-store"); headers.append("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT"); var myObj = { "firstName": personClass.firstname, "age": personClass.age, "lastName": personClass.lastname, "dateOfBirth": "null" }; return this.http.post(`${webServiceEndpoint}/person`, JSON.stringify(myObj), { headers: headers }).map((res: Response) => res.json()) .flatMap(data => { let body = data.json(); console.log(body); var myObj2 = { "personId": body, "address1": addressClass.streetname, "city": addressClass.city, "zipCode": addressClass.zipcode }; return this.http.post(`${webServiceEndpoint}/address`, JSON.stringify(myObj2), { headers: headers }).map((res1: Response) => res1.json()) }) } 

由于data不是Response对象,因此可能会失败:

let body = data.json();

I think you are using RxJS at the wrong way (you don't think reactive) (if you can use the newest version of angular): 我认为您以错误的方式使用RxJS(您不认为是反应式的)(如果可以使用最新版本的angular):

Example (very clean way to do this and you can test the code very easy): 示例(执行此操作的非常简洁的方法,您可以非常轻松地测试代码):

createPerson(personClass: User, addressClass: Address): Observable<Address> {
  /* Angular 5 you can do this into the HttpClient Interceptor */
  const headers = new Headers();
  headers.append('Content-Type', 'application/json');
  headers.append("Cache-Control", "no-cache");
  headers.append("Cache-Control", "no-store");
  headers.append("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT");

  return this.createPerson(person)
    .switchMap((person: Person) => this.http.post<Person>(`${webServiceEndpoint}/person`, { person , headers})
    .switchMap((person: Person) => createAddress(person, address))
    .switchMap((person: Person) => this.http.post(`${webServiceEndpoint}/address`, person, {
        headers: headers
      }));
}

createAddress(person: Person, address: Address): Observable<Address> {
   return Observable.of({
     "personId": person.id,
     "address1": addressClass.streetname,
     "city": addressClass.city,
     "zipCode": addressClass.zipcode
   });
}

createPerson(person: Person): Observable<Person> {
  return Observable.of(<Person>{
    "firstName": personClass.firstname,
    "age": personClass.age,
    "lastName": personClass.lastname,
    "dateOfBirth": "null"
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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