0
var reporter = []

fetch("http://localhost:3000")
  .then(response => response.json())
  .then(response => {
    response.forEach(element => {
      reporter.push(element)
    });
  })

console.log(reporter)

I have a fetch function which gets an array from port 3000, but I am unable to store it in the array.

What am I doing wrong?

Thanks for any help.

1 Answers1

0

Console log is called before the fetch is finished. Wait for the response or use console log in a following then clause

var reporter = []

fetch("http://localhost:3000")
  .then(response => response.json())
  .then(response => {
    response.forEach(element => {
      reporter.push(element)
    });
  })
  .then(console.log(reporter)
Aaron
  • 1,600
  • 1
  • 8
  • 14