title: Firebase Functions + CORS
body: <p>Having an issue with a firebase function that I need to work with cors. Based off the documentation and all the posts I've read it should be working but seem's like no matter what I try I keep getting the same error:</p>
  
  <pre><code>Failed to load &lt;URL&gt;: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 500.
  If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
  </code></pre>
  
  <p>And here is the corresponding code in my firebase functions index.js file:</p>
  
  <pre><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  const cors = require('cors')({origin: true});
  const stripe = require('stripe')('&lt;TEST_KEY&gt;');
  const gcs = require('@google-cloud/storage')({keyFilename: '&lt;PATH_TO_KEY&gt;'});
  const Easypost = require('@easypost/api');
  const api = new Easypost('&lt;TEST_KEY&gt;');
  
  admin.initializeApp(functions.config().firebase);
  
  exports.processOrder = functions.https.onRequest((req, res) =&gt; {
    cors(req, res, () =&gt; {
  
      var body = JSON.parse(req.body);
  
      if (
        !body.shipment_id       ||
        !body.items             ||
        !body.card
      ) return res.set('Access-Control-Allow-Origin', '*').send({error: true, message: 'Missing information'});
  
  
      getPrices(body.items, (err, prices, totalPrice) =&gt; {
        if (err) return res.set('Access-Control-Allow-Origin', '*').send({error: err, message: "Error"})
  
        // Create a new customer and then a new charge for that customer:
        stripe.customers.create({
          email: 'test@example.com'
        }).then((customer) =&gt; {
          return stripe.customers.createSource(customer.id, {
            source: body.card.token.id
          });
        }).then((source) =&gt; {
          return stripe.charges.create({
            amount: (totalPrice * 100),
            currency: 'usd',
            customer: source.customer
          });
        }).then((charge) =&gt; {
          return res.set('Access-Control-Allow-Origin', '*').send({error: false, message: "Success"});
        }).catch((err) =&gt; {
          console.log(err);
          return res.set('Access-Control-Allow-Origin', '*').send({error: err, message: "Error"});
        });
  
      });
    });
  });
  </code></pre>
  
  <p>Any help would be greatly appreciated :)</p>
  
  <p><strong>Edit:</strong> Just wanted to note: I've tried only setting <code>res.set('Access-Control-Allow-Origin', '*')</code> and not using the cors middleware, and I've tried not setting the header and only using cors. Neither of which worked :(</p>
  
  <p><strong>Solution:</strong> As @sideshowbarker said in a comment, my function had an error elsewhere before returning. The Access-Control-Allow-Origin was never even getting set. Once I fixed the error it was all good! Ty!</p>