I want to use firebase functions with nodemailer to receive emails when a contact form is submited. The form is being saved in my database and then i want to use it in my index.js file with a function that triggers when a new entry is registered. The following code is what i have and it appears that functions.firestore.document is not treated as a function.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const nodemailer = require("nodemailer");
admin.initializeApp();
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: gmailEmail,
pass: gmailPassword,
},
});
const contactFormPath = "contactSubmissions/{docId}";
exports.sendContactEmail = functions.firestore
.document(contactFormPath)
.onCreate(async (snap) => {
const data = snap.data();
const mailOptions = {
from: gmailEmail,
to: gmailEmail,
subject: `New Contact Form Submission: ${data.subject}`,
text: `
You have a new contact form submission!
Name: ${data.firstName} ${data.lastName}
Email: ${data.email}
Subject: ${data.subject}
Message: ${data.message}
`,
};
try {
await transporter.sendMail(mailOptions);
console.log("Email sent successfully");
} catch (error) {
console.error("Error sending email:", error);
}
});
=== Deploying to 'imidea-940fa'...
i deploying functions
i functions: preparing codebase functions for deployment
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
+ artifactregistry: required API artifactregistry.googleapis.com is enabled
+ functions: required API cloudfunctions.googleapis.com is enabled
+ functions: required API cloudbuild.googleapis.com is enabled
i functions: Loading and analyzing source code for codebase functions to determine what to deploy
Serving at port 8795
TypeError: functions.firestore.document is not a function
at Object.<anonymous> (E:\WEBSITES\imidea\emails\index.js:22:2)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Module.require (node:internal/modules/cjs/loader:1235:19)
at require (node:internal/modules/helpers:176:18)
at loadModule (E:\WEBSITES\imidea\emails\node_modules\firebase-functions\lib\runtime\loader.js:40:16)
at loadStack (E:\WEBSITES\imidea\emails\node_modules\firebase-functions\lib\runtime\loader.js:157:23)
at E:\WEBSITES\imidea\emails\node_modules\firebase-functions\lib\bin\firebase-functions.js:56:56
Error: Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
Honestly tried way too many different things to remember. Installing, reinstalling different dependancies etc. Tried to reformat the code so it is not a function, however I can't seem to get it to work when I try that.
I read that there are two different generations of firebase functions here. The functions.firestore.document is a function in v1 but not in v2 which might be the cause of the error. Could you change the import from
const functions = require("firebase-functions");
to
const functions = require("firebase-functions/v1");
changing the import might work.