You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Functions+Nodemailer触发邮件报错:functions.firestore.document非函数

问题:Firebase Functions部署时出现TypeError: functions.firestore.document不是函数

我尝试用Firebase Functions搭配Nodemailer实现功能:当联系人表单提交并保存到数据库后,触发函数向自己发送邮件。但部署时触发TypeError,提示functions.firestore.document is not 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

解决方案

1. 核心原因

这个错误是因为你使用了Firebase Functions v2.x版本,但代码仍沿用v1.x的触发器写法。v2.x对触发器的导入和定义逻辑做了调整,不再支持functions.firestore.document()这种链式调用方式。

2. 两种修复方式

方式一:适配v2.x版本修改代码

调整导入语句和触发器定义:

// 新增v2版本的Firestore触发器导入
const { onDocumentCreated } = require("firebase-functions/v2/firestore");
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const nodemailer = require("nodemailer");

admin.initializeApp();

// ... 其余配置代码保持不变 ...

// 替换原触发器定义
exports.sendContactEmail = onDocumentCreated(contactFormPath, async (event) => {
  const snap = event.data; // v2版本中通过event.data获取文档快照
  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);
  }
});

方式二:降级到v1.x版本

如果想继续使用原有代码写法,可直接降级依赖包:

npm install firebase-functions@1.x --save

3. 额外注意事项

  • 确保已正确配置Gmail环境变量:
    firebase functions:config:set gmail.email="你的Gmail邮箱" gmail.password="你的Gmail应用专用密码"
    
    (若Gmail开启两步验证,必须使用应用专用密码,不能用普通登录密码)
  • 部署前建议本地测试:
    firebase emulators:start --only functions
    
    验证代码逻辑正常后再部署,避免重复出错

内容的提问来源于stack exchange,提问作者nikobinev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 15:58:15