Consume JSON with Angular. Error

0

I am trying to consume a json with angular and it is giving me the error:

  

TS2322 error: Type 'Observable < {} | Test > ' is not assignable to type 'Observable'.     Type '{} | Test 'is not assignable to type' Test '.       Type '{}' is not assignable to type 'Test'.         Property 'date' is missing in type '{}'.

My service:

@Injectable()
export class MyService {

  constructor(private http: Http) { }
  
  private urlTo = 'http://url.com/json/';

  getData(): Observable<Prueba> {
    return this.http.get(this.urlTo)
      .map((response: Response) => <Prueba>response.json())
      .do(data => console.log('All: ' + JSON.stringify(data)))
      .catch(this.handleError);
  }

The class tries:

export class Prueba {
    public date: number;
    public name: string;
    public age: number;
}
    
asked by Findelias 07.03.2018 в 14:49
source

1 answer

0

I have found the solution to my own problem.

 getData(): Observable<Prueba> {
    return this.http.get(this.urlTo)
      .map((response: Response) => response.json())
      .do(data => console.log('All: ' + JSON.stringify(data)))
      .catch(this.handleError);
  }

The <Prueba> exceeded

On the map line. Where did I put before <Prueba>.response.json()

    
answered by 07.03.2018 / 15:20
source