Firebase Cloud Functions CORS issue

Viewed 250

I have CORS installed for firebase cloud functions.

I'm using sendgrid to send a transactional email.

I was using this EXACT code in another folder and this function was returning no problem - but I merged 2 separate documents of functions into one and now I keep getting the error...

Access to XMLHttpRequest at 'cloud function trigger' from origin 'my website' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Really confused as to what it could be...

const cors = require('cors')({ origin: true });

exports.sendRequest = 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 msg = {
      to: req.body.companyEmail,
      from: req.body.name,
      text: 'and easy to do anywhere, even with Node.js',
      html: '<strong>and easy to do anywhere, even with Node.js</strong>'
        }        
    };

    return sgMail.send(msg);

  })
  .then((response) => {
    if (response.body) {
      res.send(response.body);
    } else {
      res.end();
    }
  })

  .catch((err) => {
    console.error(err);
    return Promise.reject(err);
  });
})
})
0 Answers
Related