in my Angular app I established a service which fetches some data from a MongoDB as a json-Array. This is the code of my service:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map';
@Injectable()
export class IdeaServiceService {
private _getURL = '/api/1.0.0/ideas';
constructor(private _http: HttpClient) { }
getIdeas() {
return this._http.get(this._getURL).map((response: Response) => response.json());
}
}
In another component I try to subscribe to this services and store the fetched data into an array of Idea (respective class which I established). This is the corresponding code:
import { Component, OnInit } from '@angular/core';
import {Idea} from '../idea';
import {IdeaServiceService} from '../idea-service.service';
@Component({
selector: 'app-ideas-overview',
templateUrl: './ideas-overview.component.html',
styleUrls: ['./ideas-overview.component.css'],
providers: [IdeaServiceService]
})
export class IdeasOverviewComponent implements OnInit {
ideas: Array<Idea>;
constructor(private _ideaService: IdeaServiceService) { }
ngOnInit() {
this._ideaService.getIdeas().subscribe(resIdeaData => this.ideas = resIdeaData);
}
}
Now the following type error occurs:
Error:(19, 59) TS2322:Type 'Promise' is not assignable to type 'Idea[]'. Property 'includes' is missing in type 'Promise'.
Thanks a lot for your support!
HttpClientby default formats the response to JSON so we no longer need to parse it usingresponse.json()