vendredi 20 septembre 2019

Can't access array values in nodejs

I send an array of values to my node server to be written to a log file.

My $.ajax post works at the client side. At the back-end, I print the array to the console, but when I try to write it to the file using the code below, i get a blank output in the log file.

function log(req,res)
{
  var list = req.body;
  console.log(list);

  for(var i = 0; i < req.body.length; i++)
  fs.writeFileSync("./log.dat", JSON.stringify(list[i]) + "\r\n");
}

The array is a collection of JSON objects like this on the console.

Side Note: I can write to the file in I remove JSON.stringify though.

Any idea what I'm doing wrong?




Aucun commentaire:

Enregistrer un commentaire