i'm iterating through a csv file shipmentId.csv and it seems like the function is reading each row, but the json file that i'm writing to is not appending. It's just writing the last api response. How do i append the json file to save the entire loop of api responses?
// read csv file
const filePath = path.join(__dirname, 'shipmentId.csv');
fs.readFile(filePath, async (error, data) => {
if (error) {
return console.log('error reading file');
}
// parsedData = [{id, ShipmentId, ...}, {id, ...}, ...]
const parsedData = await neatCsv(data);
parsedData.map((rowObj) => {
// rowObj = {id, ShipmentId, ...}
let id = rowObj.ShipmentId;
// console.log(id)
(async function () {
const mwsRequestData = {
Version: '2010-10-01',
Action: 'ListInboundShipmentItems',
SellerId: config.SellerId,
MWSAuthToken: config.MWSAuthToken,
ShipmentId: id,
};
try {
let response = await mws.fulfillmentInboundShipment.search(mwsRequestData);
if (response.status == 200) {
throw new Error(`Unexpected status code ${response.status}, expected 200`);
}
console.log('response', response);
// let data = JSON.stringify(response, null, 2);
fs.writeFileSync('todo2.json', JSON.stringify(response, null, 2));
} catch (err) {
console.log('That did not go well.')
}
}());
});
});
CodePudding user response:
You are constantly rewriting the todo2.json
file in each iteration of map.
Instead you could collect all data in an array and write it to a file outside the parsedData.map()
or stream it into a file from inside the map.
You could gather references from here
CodePudding user response:
looks like i just had to change this
fs.writeFileSync('todo2.json', JSON.stringify(response, null, 2));
to this?
fs.appendFileSync('todo2.json', JSON.stringify(response, null, 2));