2021年5月6日星期四

How to add comma separator when appending JSON objects with node.js fs.appendFile?

I am looping through all images contained in a folder and for each image, I need to add its path, date (null), and a boolean into an object JSON.

This is the code:

files.forEach(file => {    fs.appendFile(      'images.json', JSON.stringify({directory: `${sourcePathDesktopWin}/${folder}/${file}`, posted: false, date: null}, null, 2), (err) => {        if (err) throw err;        console.log(`The ${file} has been saved!`);      }    );  });  

This is the result:

{    "directory": "D:/directory1/test1.jpg",    "posted": false,    "date": null  }{    "directory": "D:/directory1/test2.jpg",    "posted": false,    "date": null  }  

As you can see when appending it is not adding the comma separator between each JSON object. How can I add that?

https://stackoverflow.com/questions/67428710/how-to-add-comma-separator-when-appending-json-objects-with-node-js-fs-appendfil May 07, 2021 at 11:37AM

没有评论:

发表评论