简体   繁体   English

错误TS2339:类型'Observable <Response>'上不存在属性'map'

[英]error TS2339: Property 'map' does not exist on type 'Observable<Response>'

I am trying to get data from mongodb, for which I have written a service. 我正在尝试从mongodb获取数据,我为此编写了一项服务。 But I am getting an error like error TS2339: Property 'map' does not exist on type 'Observable<Response>' 但我得到一个错误,如error TS2339: Property 'map' does not exist on type 'Observable<Response>'

Please help me to resolve this error... 请帮我解决这个错误...

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class DataService {

  result:any;

  constructor(private _http: Http) { }

  getUsers() {
    return this._http.get("/api/users")
      .map(result => this.result = result.json().data);
  }

}

You have to import and use the map operator differently: 您必须以不同方式导入和使用map运算符:

Change 更改

import 'rxjs/add/operator/map';

to

import { map } from 'rxjs/operators';

Then, do 然后做

return this._http.get("/api/users")
      .pipe(map(result => this.result = result.json().data));

Addiontal suggestion from Vikas 来自Vikas的Addiontal建议

Migrate from the Http service to the HttpClient. 从Http服务迁移到HttpClient。 see migration guide 请参阅迁移指南

To update to HttpClient , you'll need to replace HttpModule with HttpClientModule from @angular/common/http in each of your modules, inject the HttpClient service, and remove any map(res => res.json()) calls, which are no longer needed. 要更新到HttpClient ,您需要在每个模块中用@angular/common/http替换HttpModuleHttpClientModule ,注入HttpClient服务,并删除任何map(res => res.json())调用,这些调用是不再需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误TS2339:类型“字符串”上不存在属性“默认”。** - Error TS2339: Property 'Default' does not exist on type 'string'.** 错误 TS2339:类型“CustomerTransfert[]”上不存在属性“num” - error TS2339: Property 'num' does not exist on type 'CustomerTransfert[]' 错误TS2339:类型“ HTMLElement”上不存在属性“ imageID” - error TS2339: Property 'imageID' does not exist on type 'HTMLElement' 错误TS2339:类型“ {}”上不存在属性“ forEach” - error TS2339: Property 'forEach' does not exist on type '{}' 错误TS2339:类型“ HTMLElement”上不存在属性“名称” - error TS2339: Property 'name' does not exist on type 'HTMLElement' 错误 TS2339:“温度”类型上不存在属性“摄氏度” - error TS2339: Property 'celsius' does not exist on type 'Temperature' 错误 TS2339:“特殊 []”类型上不存在属性“默认” - Error TS2339: Property 'default' does not exist on type 'Special[]' 对象数组上的 Map(错误:属性“map”在类型“{}”上不存在。TS2339) - Map on array of objects (Error: Property 'map' does not exist on type '{}'. TS2339) TS2339:类型“ {}”上不存在属性“ props” - TS2339: Property 'props' does not exist on type '{}' TS2339:“元素”类型上不存在属性“样式” - TS2339: Property 'style' does not exist on type 'Element'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM