每次向我的firestore数据库添加新用户时,我都会尝试触发一封电子邮件。以下代码是根据Youtube教程编写的,因为我不太熟悉部署函数。下面的代码是用functions/index.js编写的,但触发了一个我不确定如何修复的错误:"部署错误"。函数在加载用户代码时失败。错误消息:错误:请检查您的函数日志以查看错误原因:https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs"。完整的错误可以在代码下面找到。谢谢
const functions = require("firebase-functions");
const nodemailer = require("nodemailer");
const admin = require("firebase-admin");
admin.initializeApp();
require("dotenv").config();
const { SENDER_EMAIL, SENDER_PASSWORD } = process.env;
exports.sendEmailNotification = functions.firestore
.document("submissions/{docId}")
.onCreate((snap, ctx) => {
const data = snap.data();
let authData = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: { user: SENDER_EMAIL, pass: SENDER_PASSWORD },
});
authData
.sendMail({
from: "developer@chutoro.app",
to: `${data.email}`,
subject: `Submssion Accepted`,
text: `${data.email}`,
})
.then((res) => console.log("Successfully sent mail"))
.catch((err) => console.log(err));
});
+ functions: Finished running predeploy script.
i firestore: reading indexes from firestore.indexes.json...
i cloud.firestore: checking firestore.rules for compilation errors...
! [W] undefined:undefined - Ruleset uses old version (version [1]). Please update to the latest version (version [2]).
+ cloud.firestore: rules file firestore.rules compiled successfully
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
+ functions: required API cloudbuild.googleapis.com is enabled
+ functions: required API cloudfunctions.googleapis.com is enabled
i firestore: latest version of firestore.rules already up to date, skipping upload...
+ firestore: deployed indexes in firestore.indexes.json successfully
i functions: preparing functions directory for uploading...
i functions: packaged functions (40.2 KB) for uploading
+ functions: functions folder uploaded successfully
+ firestore: released rules firestore.rules to cloud.firestore
i functions: updating Node.js 10 function sendEmailNotification(us-central1)...
! functions[sendEmailNotification(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs
我添加了"nodemailer":"6.4.11";到依赖项,它现在对我有效