I'm trying to fetch this JSON in my javaScript code using the fetch API:
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
]
}
This is my code:
const endpoint = 'http://localhost:3000/posts'
let posts = []
fetch(endpoint)
.then(resp => resp.json())
.then(data => posts.push(...data))
console.log(posts[0])
The console.log gives me undefined on chrome's console, but if I type posts[0] on the same console I get the post object I'm hoping to get.
I'm serving the JSON file with json-server and the .html with the vs code extension live-server.