修复属性“地图”在类型“对象”上不存在

时间:2018-08-06 11:30:39

标签: angular typescript angular6

我正在使用Angular CLI: 6.0.8,并在下面实现了以下服务。问题是我的代码编辑器linter继续显示错误,如下所示:

[ts] Property 'map' does not exist on type 'Object'.
any

错误是在具有.map()函数的return语句上:

............

return bills.map((bill) => new Bill(bill));

........

服务

// imported the HttpClient to make the HTTP requests.
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

import { Bill } from '../../models/bill'

import { Observable } from  'rxjs/Observable';

import  'rxjs/add/operator/catch';

import  'rxjs/add/operator/map';




// Transform this TypeScript class into an injectable service.
@Injectable()
export class BillServiceProvider {
  // url: string = 'https://localhost:8000/api';
  // Hold the address of your back-end API.
  baseUrl:string = "http://localhost:8000/api/properties";
// Add, 'Http' injection in the constructor.
  constructor(private http: HttpClient) {
    // console.log('Hello BillServiceProvider Provider');
  }

  // Sending a GET request to /bills
  public  getBills(): Observable<Bill[]> {
    return this.http
    .get(this.baseUrl + '/bills')
    .map(bills => {
      return bills.map((bill) => new Bill(bill));
    })
    .catch((err)=>{
      console.error(err);
    })

  }

3 个答案:

答案 0 :(得分:-1)

您可以使用接口,类等指定要返回的类型。在这种情况下,您需要使用 return this.http.get<Bill[]>

public  getBills(): Observable<Bill[]> {
    return this.http.get<Bill[]>(this.baseUrl + '/bills')
    .map(bills => {
      return bills.map((bill) => new Bill(bill));
    })
    .catch((err)=>{
      console.error(err);
    })

答案 1 :(得分:-1)

尝试此代码

// imported the HttpClient to make the HTTP requests.
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

import { Bill } from '../../models/bill'

import { Observable } from  'rxjs/Observable';

import  'rxjs/add/operator/catch';

import  'rxjs/add/operator/map';

// Transform this TypeScript class into an injectable service.
@Injectable()
export class BillServiceProvider {
  // url: string = 'https://localhost:8000/api';
  // Hold the address of your back-end API.
  baseUrl:string = "http://localhost:8000/api/properties";
// Add, 'Http' injection in the constructor.
  constructor(private http: HttpClient) {
    // console.log('Hello BillServiceProvider Provider');
  }

  // Sending a GET request to /bills
  public  getBills(): Observable<Bill[]> {
    return this.http
    .get(this.baseUrl + '/bills')
    .map(bills => {
      return bills.map((bill) => {
       const billObj = new Bill(bill);
       return billObj;
      });
    })
    .catch((err)=>{
      console.error(err);
    })

  }

答案 2 :(得分:-1)

版本5.5 开始,新方法是使用"pipeable operators"

  

myObservable   .pipe(地图(数据=>数据))      .subscribe(...);