【发布时间】:2018-07-08 12:43:27
【问题描述】:
我有以下 DataService,它应该以链的形式调用以下 API,因此我能够将前一个的响应传递给下一个。这是我到目前为止的开始方式,但是当我尝试访问响应对象的属性之一时出现以下错误:
为什么会这样,因为当我console.log响应时,它有如下结构:
{
"data": "",
"id": ""
}
“对象”类型上不存在属性“数据”。
import { Injectable } from '@angular/core';
import { Person } from './entities/person';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/toPromise';
import { mergeMap } from '../../node_modules/rxjs/operator/mergeMap';
import { Observable } from '../../node_modules/rxjs/Observable';
@Injectable()
export class DataService {
results: Person[];
apiUrl1: string = 'http://localhost:3000/person/';
apiUrl2: string = 'http://localhost:3000/facility';
apiUrl3: string = 'http://localhost:3000/exposure';
constructor(private http: HttpClient) {
}
createPerson(person: Person): Observable<any> {
return (this.http.post(this.apiUrl1, person)
.map(response => this.http.post(this.apiUrl2, response.data)));
}
【问题讨论】:
标签: angular httpresponse chaining