0

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 if I remove JSON.stringify and [i] though.

Any idea what I'm doing wrong?

Edit:

I did this and I get "undefined" in my log file.

try {
    for(var i = 0; i < 3; i++)
    fs.writeFileSync("./log.dat", list[i] + "\r\n");
} catch (err) {
    console.log('Error writing Metadata.json:' + err.message)
}

I've deduced it's something to do with the loop and the array. I can't reference the array by index.

My ajax req code:

var items = {logs: foghorn_items};
      path = server + "Foghorn/log";
      $.ajax({
      url: path,
      method: 'POST',
      dataType: 'JSON',
      data: items,
      success: function (data){
        alert('Logs sent to file');
      }
);

UPDATE

For some reason the loop doesn't work with list.length and req.body.length.

3 Answers 3

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

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

Try the above one

Sign up to request clarification or add additional context in comments.

11 Comments

can you show the body data? is that above link in your question?
Yes, that's body data that's getting printed from console.log(list)
what is printing in console which i have posted?
what is printing in console now?
@ShaneD'Silva what did you get from line console.log(typeof req.body) and console.log(typeof req.body.logs)
|
1

Use try/catch, to show error:

try {
    fs.writeFileSync("./log.dat", JSON.stringify(list[i]) + "\r\n");
} catch (err) {
    console.log('Error writing Metadata.json:' + err.message)
}

3 Comments

what access rights to the file? and check the path to file
See the last line of my question
Thought about it, but could not to check it in my side
0

use appendfile method

fs.appendFile("./log.dat", JSON.stringify(list[i]) + "\r\n",function(err){   
});

when you are using writeFileSync, in log file it shows last value written. which might be blank.

2 Comments

i get "undefined" for list[i]. But if i do console.log(list), it prints all the values
for(var i = 0; i < req.body.length; i++) fs. appendFile("./log.dat", JSON.stringify(req.body[i]) + "\r\n",function(err){ });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.