I've created a Google Cloud Function which, when it is trigged, it creates an xlsx file with exceljs and attaches it in an email sent with firebase-send-mail. This is my code:
(data is dummy for test)
exports.onEventReservCreate = functions
.region("europe-west2")
.firestore.document(
"foodCourts/{foodCourtId}/events/{eventId}/eventReservations/{evtResId}"
)
.onCreate(async (snap, context) => {
try {
const excel = require("exceljs")
//Creating New Workbook
var workbook = new excel.Workbook()
//Creating Sheet for that particular WorkBook
var sheet = workbook.addWorksheet("Sheet1")
// set path for file
const tempFilePath = path.join(os.tmpdir(), "excel.xlsx")
sheet.columns = [
{ key: "name", header: "name" },
{ key: "age", header: "age" },
]
var data = [
{ name: "Eddy", age: 24 },
{ name: "Paul", age: 24 },
]
//adding each in sheet
data.forEach(el => sheet.addRow(el))
// get the user email from firestore db
const userRef = db.collection(`users`).doc(uid)
const user = (await userRef.get()).data()
workbook.xlsx
.writeFile(tempFilePath)
.then(res => {
// sending email to user
const emailData = {
to: [user.email],
template: {
name: "reportEvents",
data: {
attachmentPath: tempFilePath,
attachmentName: "nome allegato",
date: dateFnsTz.format(
new Date(),
"dd/MM/yyyy - HH:mm"
),
},
},
}
return db.collection("email").doc().set(emailData)
})
.catch(err => console.log("ERROR --> ", err))
} catch (err) {
console.log(
`Error while sending - Error: ${err}`
)
}
})
In functions log i have this error:
Error when delivering message=email/[id]: Error: ENOENT: no such file or directory, open '/tmp/excel.xlsx'
Why /tmp folder doesn't exist?
Thanx