属性“地图”在类型“对象”上不存在-Ionic 3

时间:2018-07-24 14:57:36

标签: ionic3 httprequest

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from  'rxjs/Observable';
import 'rxjs/add/operator/map';


@Injectable()
export class User {
  id: number;
  name: string;

  constructor(values: Object = {}){
    Object.assign(this, values);
  }
}
export class RestProvider {

  apiUrl:string = "http://localhost:3000";

  constructor(public http: HttpClient) {
    console.log('Hello RestProvider Provider');
  }

  public  get(): Observable<User[]> {
    return this.http.get(this.apiUrl + '/users').map(users  => {
      return users.map((user) => new  User(user)); //Property 'map' does not exist on type 'Object'.
        }, err => {
          console.log(err);
        });
      }
  public post(user: User): Observable<User>{
    return this.http.post(this.apiUrl + '/users', user).map(response => {
      return new User(response);
    }, err => {
      console.log(err);
    });
  }
  public put(user: User): Observable<User> {
    return this.http.put(this.apiUrl + '/users/' + user.id, user).map(response => {
      return new User(response);
    }, err => {
      console.log(err);
    });
  }
    public delete(userid: number) {
      return this.http.delete(this.apiUrl + '/users/' + userid), 
      err => {
          console.log(err);
        };
  }

}

大家好,

我是离子3的入门者。 我不知道如何解决这种情况?我试图创建一个带有http请求的rest api。我也是从这里作弊:https://www.techiediaries.com/ionic-http-client/

我认为问题在于该行上还有第三个属性。所有其他人都有一两个。所以我应该在某些地方添加一些东西?

1 个答案:

答案 0 :(得分:1)

我想我解决了。这是我解决问题的方法。

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from  'rxjs/Observable';
import 'rxjs/add/operator/map';

export class User { //Put the class out if @injectable
      id: number;
      name: string;

      constructor(values: Object = {}){
        Object.assign(this, values);
      }
    }

@Injectable()
export class User {
  id: number;
  name: string;

  constructor(values: Object = {}){
    Object.assign(this, values);
  }
}
export class RestProvider {

  apiUrl:string = "http://localhost:3000";

  constructor(public http: HttpClient) {
    console.log('Hello RestProvider Provider');
  }

  public  get(): Observable<User> {  //Changed the way I handled the get request a little bit
    return this.http.get(this.apiUrl + 'users').map(users  => {
  return users as User;
        }, err => {
          console.log(err);
        });
      }
  public post(user: User): Observable<User>{
    return this.http.post(this.apiUrl + '/users', user).map(response => {
      return new User(response);
    }, err => {
      console.log(err);
    });
  }
  public put(user: User): Observable<User> {
    return this.http.put(this.apiUrl + '/users/' + user.id, user).map(response => {
      return new User(response);
    }, err => {
      console.log(err);
    });
  }
    public delete(userid: number) {
      return this.http.delete(this.apiUrl + '/users/' + userid), 
      err => {
          console.log(err);
        };
  }

}