I am analyzing a JSON file, expecting it to return Message[] using a promise. This code is similar to the one utilized in the Heroes sample project found in HTTP documentation
{
"data": [
{"id":"1","commid":"0","subject":"test1subject","body":"test1 body"},
{"id":"2","commid":"1","subject":"lkjhlkjh","body":"nbvjhg"}
]
}
Instead of returning an array of data with objects, it should return as Message[]. How can I adjust this to function like the example? It must return a Message[]
return this.http.get('messages.json')
.toPromise()
.then(this.extractData)
.catch(this.handleError);
private extractData(res: Response) {
let body = res.json();
console.log(body);
return body.data || { };
}
Here is how the code looks in my component
export class Message {
id: number;
commid: number; // community ID that this message belongs to
subject: string;
body: string;
}
msgs: Message[];
this.msgService.getMessages().then(messages => {
this.msgs = messages;
});
For illustration purposes, here is a link to the Angular doc sample: http://plnkr.co/edit/KZGeyULqrcuZDSeg0CkF?p=preview