Property 'map' does not exist on type 'Object' - Ionic 3

Pragma
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);
        };
  }

}

Hi everyone,

I'm a starter to ionic 3. I couldn't figure out how to solve this situation? I was trying to create a rest api with http requests in it. Also I was cheating from here: https://www.techiediaries.com/ionic-http-client/

I think the problem is that there is a third property on that line. All others have one or two. So I should add something to somewhere but where?

Pragma

I think I solved it. Here is how I solved the problem.

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);
        };
  }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive