I implemented the code below and it works perfectly fine when I run it in on local machine with npm start
However, when I deploy it on Firebase, the sendOrderEmail function doesn't work.
I don't even get any logs.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const {email, password} = require('./config');
const nodemailer = require('nodemailer');
const htmlToText = require('nodemailer-html-to-text').htmlToText;
admin.initializeApp();
const mailTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: email,
pass: password
}
});
mailTransport.use("compile", htmlToText());
const APP_NAME = 'the.Travelest';
exports.sendUserEmail = functions.database
.ref("/lettersRequest/{pushId}")
.onCreate(request => {
return sendOrderEmail(request.val().email);
});
async function sendOrderEmail(email){
const mailOptions = {
from: `${APP_NAME}`,
to: email,
subject: `Your order from ${APP_NAME}`,
html: `
<div>
<strong> Hello </strong>
World
</div>
`
};
return await mailTransport.sendMail(mailOptions);
}
UPDATE After checking the logic, I believe that I've found the issues in another place.
So, the logic is - when I click on SUBMIT in form, I send the data to Firebase Runtime Database. And this action triggers the function above and send the email.
However, the SUBMIT button doesn't trigger the process it should trigger. I don't see even console.log result in the console, when I click on SUBMIT
Again, it works locally, when I run nom start
Here is the submit handler:
const handleSubmit = event => {
event.preventDefault();
setErrors(validate(values))
setIsSubmitting(true)
try{
const newRequestRef = database.ref('lettersRequest').push();
const newRequest = {
country: values.country,
travelersAmount: values.travelersAmount,
introExtro: values.introExtro,
comments: values.comments,
email: values.email,
phone: values.phone
};
newRequestRef.set({
email: values.email,
request: newRequest
});
console.log("no error")
}catch(e){
console.log("error")
}
}
You are not handling the promise returned by set(). Try this:
const handleSubmit = event => {
event.preventDefault();
setErrors(validate(values))
setIsSubmitting(true)
const newRequestRef = database.ref('lettersRequest').push();
const newRequest = {
country: values.country,
travelersAmount: values.travelersAmount,
introExtro: values.introExtro,
comments: values.comments,
email: values.email,
phone: values.phone
};
newRequestRef.set({
email: values.email,
request: newRequest
}).then(() => {
console.log("Request Added in Database")
}).catch(e => console.log(e));
}
If it logs 'Request Added in Database' then it has been added successfully and your function should trigger.
Additionally, this can also be done by a Callable Function which means you call the function directly from the client, do all the validation, add the data in database and send the email instead of using database triggers.
So, the issue was even more trivial than I could expect :D I did deploy to firebase, but I've never did commit and build project... As a result, nothing was updated after each deploy.
Stupid mistake