gpt4 book ai didi

javascript - Firebase 云函数使用 fetch() 反复循环

转载 作者:行者123 更新时间:2023-12-01 02:24:26 24 4
gpt4 key购买 nike

我是第一次使用 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;
});
}
});

最佳答案

通常,如果您的 HTTPS 触发函数被调用多次,则意味着您没有发回响应。发送响应是 Cloud Functions 环境知道您已完成的方式,因此它可能会假设出现问题并重试:

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);
});
}
});

关于javascript - Firebase 云函数使用 fetch() 反复循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48924462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com