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

查看:33
本文介绍了错误:.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 导入 map 操作符,如下所述:

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:

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

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