I am trying to export a csv file (pulling the data from mongodb). My frontend is ionic (angular) and backend is Azure function (nodejs), I want the frontend to be able to download this csv file.
My code:
const { db, connection } = await createMongoClient();
const collection = db.collection("dummy");
try {
const data = await collection.find({}).toArray();
connection.close();
const ws = fs.createWriteStream(Date.now() + ".csv");
fastcsv
.write(data, { headers: true })
.on("finish", function () {
console.log("Write CSV successfully!");
})
.pipe(ws);
res.status(201).json({ success: true });
} catch (err) {
res.status(500).json({ err, success: false });
}
The above creates a csv file but its not downloaded by the frontend.