1

I have this block of code here:

this._auth.getToken().flatMap(token => {
  return this._http.post("/authenticate");
}).flatMap(res => {
  let headers = new Headers();
  headers.append("Content-Type", "application/json");
  headers.append("Authorization", res.json().Token);
  return this._http.get(this.endpoints[endpoint], {headers: headers});
});

http.get and http.post return an Observable<Response> which contains a .json method

However flatMap returns an Observable<Any> which doesn't contain the .json method.

It works correctly, but TSlint complains with:

Property 'json' does not exist on type '{}'.at line 32 col 49

Am I doing something wrong? Is this expected? It's not a big deal but maybe it's a symptom of bigger issues in the code.

4

1 回答 1

0

要解决此问题,您应该添加Response作为返回值的类型

flatMap((res: Response) => {
  let headers = new Headers();
  headers.append("Content-Type", "application/json");
  headers.append("Authorization", res.json().Token);
  return this._http.get(this.endpoints[endpoint], {headers: headers});
});

笔记

这已经修复了,请检查此评论,所以您可能不是最新版本。我使用 beta.0 对其进行了测试,即使不添加Response类型也无法重现此消息。

参考

于 2015-12-26T23:31:27.783 回答