我第一次使用Firebase功能。而且我不确定我做错了什么。我将我的功能称为1次,如我的网络标签中所示。但是,当我执行我的http请求到我的云功能时,它一遍又一遍地循环。
我的功能是在我的网站上处理我的联系表格,并将包含内容的表格发送给我自己。我已经尝试在调用函数时设置变量,但这对我不起作用。
有人可以告诉我这里我做错了吗?
我如何调用我的功能:
sendEmail(url, {
name: this.state.name,
email: this.state.email,
message: this.state.message,
key: "env variable here"
}) {
return fetch(url, {
body: JSON.stringify(data),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
},
method: "POST",
mode: "no-cors"
}).then(response => response.json()); // parses response to JSON
}
我的FireBase云功能:
const functions = require("firebase-functions");
const nodemailer = require("nodemailer");
const secureCompare = require("secure-compare");
const mailTransport = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: "myemail@gmail.com",
pass: "mypw"
}
});
exports.sendEmail = functions.https.onRequest((request, response) => {
const data = JSON.parse(request.body);
const key = data.key;
let hasBeenCalled = false;
// Exit if the keys don't match
if (!secureCompare(key, functions.config().cron.key)) {
console.log("The key provided in the request does not match the key set in the environment. Check that", key, "matches the cron.key attribute in `firebase env:get`");
response
.status(403)
.send(
'Security key does not match. Make sure your "key" URL query parameter matches the ' + "cron.key environment variable.");
return null;
}
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const email = emailRegex.test(data.email) ? data.email : null;
const mailOptions = {
from: data.name + " <test@gmail.com>",
to: "myemail@gmail.com",
bcc: email,
subject: "this is a message",
text: data.message
};
if (!hasBeenCalled) {
mailTransport
.sendMail(mailOptions)
.then(() => {
this.hasBeenCalled = true;
console.error("email sent to: " + mailOptions.to);
console.error("email sent from: " + data.name);
})
.catch(error => {
this.hasBeenCalled = true;
});
}
});
答案 0 :(得分:2)
通常,如果多次调用HTTPS触发的函数,则表示您未发送回应。发送响应是云功能环境如何知道您已完成的,因此可能会出现问题并重试:
exports.sendEmail = functions.https.onRequest((request, response) => {
const data = JSON.parse(request.body);
const key = data.key;
let hasBeenCalled = false;
// Exit if the keys don't match
if (!secureCompare(key, functions.config().cron.key)) {
console.log("The key provided in the request does not match the key set in the environment. Check that", key, "matches the cron.key attribute in `firebase env:get`");
response
.status(403)
.send(
'Security key does not match. Make sure your "key" URL query parameter matches the ' + "cron.key environment variable.");
return null;
}
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const email = emailRegex.test(data.email) ? data.email : null;
const mailOptions = {
from: data.name + " <test@gmail.com>",
to: "myemail@gmail.com",
bcc: email,
subject: "this is a message",
text: data.message
};
if (!hasBeenCalled) {
mailTransport
.sendMail(mailOptions)
.then(() => {
this.hasBeenCalled = true;
console.log("email sent to: " + mailOptions.to);
console.log("email sent from: " + data.name);
response.status(200).send("Mail sent");
})
.catch(error => {
console.error(error);
this.hasBeenCalled = true;
response.status(500).send(error);
});
}
});