尝试使用Firebase功能发送电子邮件时不断出错

时间:2018-09-06 14:39:16

标签: javascript node.js firebase google-cloud-functions sendgrid

有人能帮忙吗?这总是给我一个错误。有什么建议吗?我收到以下错误:错误:无法处理该请求。我已删除敏感信息。

const functions = require('firebase-functions');

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey("API KEY"); /*PLACEHOLDER KEY*/

const cors = require('cors')({
  origin: ['https://DOMAIN.co.uk'], 
  methods: ['POST', 'OPTIONS'],
  allowedHeaders: ['Content-Type'],
  preflightContinue: false,
  optionsSuccessStatus: 204
});



exports.sendEmailConfirmation = functions.https.onRequest((req, res) => {
  
  cors(req, res, () => {
    
    return Promise.resolve()
      .then(() => {
        if (req.method !== 'POST') {
          const error = new Error('Only POST requests are accepted');
          error.code = 405;
          throw error;
        }
  
        const message = {
          to: 'email@email.com',
          from: 'email@email.com',
          subject: 'subject',
          text: 'some text',
          html: 'some html'
        };
    
        return sgMail.send(message);
        
      })
      .then((response) => {
        if (response.body) {
          res.send(response.body);
        } else {
          res.end();
        }
      })
      .catch((err) => {
        console.error(err);
        return Promise.reject(err);
      });
      
  });
  
});

它通常使用函数加载,并且在那里没有显示任何错误,我无法弄清为什么它不起作用。

非常感谢您的建议

0 个答案:

没有答案