I am new to the exporting CSV from JSON... I am pretty close to gettin this... but I dont know how to send the CSV back to the client from my express server.
The user selects the customers to export, and hits the export button... I'd like the CSV file to be sent back.
I got the JSON data converted to CSV on the server, how would I send this file back to the client?
Here is my express route so far -
router.post('/SaveCSV', (req, res) => {
const csv = JSONToCSV(req.body, { fields: ["Customer Name", "Business Name", "Customer Email", "Customer ID", "School ID", "Student Count", "Admin Count", "Courses Count" ]})
})
I have tried res.send, res.sendFile, and res.attachment.. any guidance?
Update***
router.post('/SaveCSV', (req, res) => {
const csv = JSONToCSV(req.body, { fields: ["Customer Name", "Business Name", "Customer Email", "Customer ID", "School ID", "Student Count", "Admin Count", "Courses Count" ]})
res.attachment('CustomerData.csv').send(csv)
})
Here is my fetch call on the client --
const saveCsv = async (customers) => {
const token = await getAccessTokenSilently();
try {
const response = await fetch('/api/SaveCSV', {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
"content-type": 'application/json; Charset=UTF-8'
},
body: JSON.stringify(customers)
})
const responseData = await response.json()
console.log(responseData)
} catch (error) {
console.log(error)
}
}