I need to retrieve an Excel file from Box.com using their API and then convert the Excel data into JSON format. Afterwards, I plan to display this JSON data using Express and Handlebars.
Below is the function I've created for fetching the Excel file and converting it to JSON:
let getExternalData = async () => {
const stream = await client.files.getReadStream('123456789');
const externalFile = await fs.createWriteStream('/external.xlsx');
stream.pipe(externalFile);
let finished = await externalFile.on('finish', async () => {
const excelData = await excelToJson({
source: fs.readFileSync(externalFile.path),
header: {
rows: 1
},
columnToKey: {
A: "edate",
B: "esource"
},
sheets: ['Sheet1']
});
console.log(excelData);
return excelData;
})
}
My current issue is that I am unsure how to handle the pending Promise when calling the function like this:
let testData = getExternalData();
console.log(testData); // Promise { <pending> }
I intend to use this function to download multiple files and store them in variables to be passed to my express route later on.
app.get('/', (req, res) => {
res.render('stories.ejs', {
msg: "Welcome to stories.ejs",
odata: odata,
edata: edata,
...
});
});
Would wrapping it in an anonymous async function be a solution?