Skip to content
Advertisement

React.Js and Typescript how to read data from JSON?

everyone.

I am using MongoDB with mongoose and React.Js. I have a page where the user can display a post.

I am sending fetch request to the backend where I am getting this json data as response.

JavaScript

And on the frontend I am using Fetch API, to get this data, what I am trying to do is I want to be able to read every single key and value from the JSON response as I want to use this data to display title, content etc…

JavaScript

Now I had tried using the getPost.map(…….), however I got error that said getPost.map is not a function event when I did setPost(responseData.post) I got the same error. So how can I access different data in the JSON response ?

In case this helps here is my sendRequest function. and this is my sendRequest that is located in totaly different file

JavaScript

Advertisement

Answer

Your return Object would look something like this:

JavaScript

To simplify your work and make sure you get the correct data back, you should consider doing this below:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement