I am making exernal api calls which return json objects. After all calls are made I want to write the collection of json objects to file. However when I do this it is writing to file in the wrong format.
EDIT: the findPrices() function is being called inside a loop.
priceSearch1Array = [];
function findPrices(res) {
(api.get({
origin: A,
destination: B,
}).then(function(response) {
priceSearchVar = JSON.stringify(response.result.data);
priceSearch1Array.push(priceSearchVar);
}).catch(function(error) {
console.log('error and continue' + error);
}))
}
After all API calls, the array is sent to file.
fs.writeFileSync('api/data/destinations3.json', priceSearch1Array);
The current output is example :
[{flight:"data", segments: { price:"23.22"}}],
[{flight:"data", segments: { price:"78.45"}}],
[{flight:"data", segments: { price:"48.45"}}]
When i need it to write to file in the format :
[ {flight:"data", segments: { price:"23.22"}},
{flight:"data", segments: { price:"78.45"}},
{flight:"data", segments: { price:"48.45"}} ]
I need the json objects in a list format and then add to file rather than an array per object. Is there a way to do this?
response.result.datais actually a single-item array, and not just the item itself?