React 联系表中的 CORS 错误 Firebase Cloud Functions

CORS Error Firebase Cloud Functions in React Contact Form

我有一个 React 联系表单,使用 firebase 云功能处理发送带有 nodemailer 的电子邮件和一个 axios post 请求以将数据存储在我的 firestore 数据库中。我在我的函数中使用了 CORS,但在提交表单时我的控制台中仍然出现 CORS 错误。

来自来源“http://localhost:3000”的 'https://.cloudfunctions.net/submit' 处的 XMLHttpRequest 访问已被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' header。我花了大约 2 个小时在谷歌上搜索,从我读到的内容看来,如果我有 const cors = require('cors')({origin: true});它应该工作。任何帮助都会很棒。

const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
const cors = require('cors')({origin: true});
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;

const mailTransport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: gmailEmail,
    pass: gmailPassword,
  },
});

exports.submit = functions.https.onRequest((req, res) => {
  cors(req, res, () => {
    if (req.method !== 'POST') {
      return;
    }
    const mailOptions = {
      from: req.body.email,
      replyTo: req.body.email,
      to: gmailEmail,
      subject: `${req.body.name} submitted a quote request!`,
      text: req.body.message,
      html: `<p>{req.body.message}</p>`,
    };
    mailTransport.sendMail(mailOptions).then(() => {
      console.log('New email sent to:', gmailEmail);
      res.status(200).send({ isEmailSend: true });
      return;
    });
  });
});


// This snippet is in my contact.js component
const sendEmail = () => {
    axios
      .post(
        'https://<myurl>.cloudfunctions.net/submit',
        formData
      )
      .then((res) => {
        db.collection('emails').add({
          name: formData.name,
          email: formData.email,
          phone: formData.phone,
          message: formData.message,
          time: new Date(),
        });
      })
      .catch((err) => {
        console.log(err);
      });
  };

我删除了云函数并重新创建它,我不再有 cors 问题。