angular - 使用 Angular 链接的服务端点
问题描述
我有以下 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)));
}
解决方案
Typescript 严格检查响应中的数据属性,只需将响应类型更改为 any,
.map((response : any) => this.http.post(this.apiUrl2, response.data)));
推荐阅读
- reactjs - OrderBy 和 where 查询在 firebase 中不能同时工作
- javascript - reactjs + typescript 出现错误:类型“字符串”不可分配给类型“从不”
- python - Python - 芹菜自动更新
- python - 与超类型不兼容的签名 - 用于强制命名的抽象方法与 Liskov
- firefox - 无法使用 Firefox 在本地测试 WebAssembly 页面 - 未定义 SharedArrayBuffer
- magento2 - 每个产品仅第一次触发 addToCart 事件
- sql - sqlite upsert 查询语法?
- node.js - 如何在单元测试用例的 sequelize-mock 中模拟数据?
- r - 如何解决 R 中的朴素贝叶斯分类器绘图问题?
- powerbi - 缩小字段按钮添加