Mike3355
Mike3355

Reputation: 12061

Using Observable to initialize an interface in Angular 2

I have a interface called Users and I am trying to initialize the object like the below:

constructor(private _http: Http) {  }

  getUsers(): Observable<User[]> {
        return this._http.get(this._url)
            .map((response: Response) => response.json())

            .do(data => console.log("User data" + JSON.stringify(data)))
            .catch(this.handleError);
    }

    private handleError(error: Response) {
        console.log(error);
        return Observable.throw(error.json().error || 'Internal Server error');
    } 

However I am getting the following errors.

The type argument for type parameter 'T' cannot be inferred from the 
usage. Consider specifying the type arguments explicitly. Type 
argument candidate 'Response' is not a valid type argument because it 
is not a supertype of candidate 'Response'. Types of property 'type' 
are incompatible. Type 'string' is not assignable to type  'ResponseType'

The URL is the file path to a JSON file. I am trying to set it up this way so I can easier transition to a real HTTP call but right now I want to use mock data from a JSON file.

Upvotes: 1

Views: 1031

Answers (1)

micronyks
micronyks

Reputation: 55443

Import Response object from angular2/http

import { Response } from '@angular/http';

You can read or understand more : https://angular.io/docs/ts/latest/guide/server-communication.html#!#extract-data

Upvotes: 5

Related Questions