Skip to content Skip to sidebar Skip to footer

Angular 2 Http Service Not Returning Promise

I'm trying to get an angular 2 service to retrieve data from an HTTP request and return it as a promise. When I use the service in the component, the data I'm passing from the ser

Solution 1:

response.json() already gives you back the data object of your response as JSON, so remove the .data property access.

Solution 2:

When you response.json() the result is the exact content from the response of the request you made.

In this case, https://jsonplaceholder.typicode.com/posts returns an array (if open the url in a browser you'll see the array): [{...}, {...}, ...].

Solution 3:

From response.json().data remove .data and add || {} if body is null

Finally:

.then((response: Response) => response.json() || {})

Post a Comment for "Angular 2 Http Service Not Returning Promise"