错误:.map()不是函数 [英] Error : .map() is not a function

查看:203
本文介绍了错误:.map()不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从组件订阅HTTP-GET的已解析响应。获取错误说.map不是一个函数,无论是从HTTPService使用还是从组件类中使用。

Trying to subscribe to the parsed response of HTTP - GET from the component. Getting a error saying .map is not a function whether used from HTTPService or from the component class.

Httpdemo.getResponse().map(res => res.json()).subscribe((cities)=>{ 
   this.cities = cities.cities;          
   console.log(this.cities);
});

但以下作品:

Httpdemo.getResponse().subscribe((cities)=>{ 
    this.cities = JSON.parse(cities._body).cities;          
    console.log(this.cities);
});

解析在Httpdemo服务中也不起作用。

The parsing also does not work in the Httpdemo service.

return this.http.get('./app/cities.json').map(res => res.json())

我的代码中的用法有什么问题?

What is wrong with the usage here in my code?

推荐答案

您需要为Observable导入地图运算符,如下所述:

You need to import the map operator for Observable as described below:

import 'rxjs/add/operator/map';

默认情况下,不包括所有运营商。

By default not all operators are included.

有关详细信息,请参阅此答案:

See this answer for more details:

  • Angular 2 HTTP GET with TypeScript error http.get(...).map is not a function in [null]

这篇关于错误:.map()不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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