Firebase 功能/Nodemailer 在提交表单时向我自己发送电子邮件

问题描述 投票:0回答:1

我想使用 firebase 功能和 nodemailer 在提交联系表单时接收电子邮件。该表单保存在我的数据库中,然后我想在我的 index.js 文件中使用它,并使用一个在注册新条目时触发的函数。以下代码是我所拥有的,并且看起来functions.firestore.document不被视为函数。

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

老实说,尝试了太多不同的事情来记住。安装、重新安装不同的依赖项等。尝试重新格式化代码,使其不是一个函数,但是当我尝试这样做时,我似乎无法让它工作。

reactjs node.js firebase google-cloud-functions nodemailer
1个回答
0
投票

我读到有两代不同的 Firebase 函数这里

您可以更改导入来源吗

const functions = require("firebase-functions");

const functions = require("firebase-functions/v1");

可能有用。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.