0

I was making a site for a personal project when I ran into an error. The client requests for the list of directories and the server returns a JSON file back to the client but the client brings up the error 'syntaxerror: unexpected end of input'. I checked the server output and it says it sent the file successfully, and when I accessed the server through the web manually, I got a valid JSON output (I checked it online). Is this a problem with the client?

server code:

app.get('/votes', (req, res) => {
  fs.readFile(path.resolve(__dirname, 'testing.json'), (err, data) => {
    if (err) {
      res.status(500).send('Error reading file');
    } else {
      res.status(200).json(JSON.parse(data));
    }
  });
});

client code:

async function dataFetch() {
  await fetch('https://serverIP/votes', {
    method: 'GET',
    mode: "no-cors",
    headers: {
      'Content-Type': 'application/json'
    }
  })
    .then(response => response.json())
    .then(data => {
      console.log(`${data} <-- data`);
      jsonFile = data;
    })
    .catch(error => {
      alert("an error has occured: "+error)
      console.log(`Error: ${error}`);
}

I tried using other methods of fetching, but none were successful.

  • 1
    `mode: "no-cors",` means, you don't want the response body: https://developer.mozilla.org/en-US/docs/Web/API/Request/mode – jabaa Feb 01 '23 at 04:26

1 Answers1

-1

i think you missed the } at the end.

  await fetch('https://serverIP/votes', {
    method: 'GET',
    mode: "no-cors",
    headers: {
      'Content-Type': 'application/json'
    }
  })
    .then(response => response.json())
    .then(data => {
      console.log(`${data} <-- data`);
      jsonFile = data;
    })
    .catch(error => {
      alert("an error has occured: "+error)
      console.log(`Error: ${error}`);
})}```