I got on my project at least 12 cloud functions on which most of them are onRequest
ones and they work perfectly. However, I just created a new one that I'm getting cors errors. Tried a bunch of things and it doesn't work.
This is what I have:
import * as functions from "firebase-functions";
import fetch from "node-fetch";
export const trkFun = functions.https.onRequest(
async (request, response) => {
const trackingNumber = request.body.trackingNumber;
const responseBody = await fetch(endPoint);
const res = await responseBody.json();
response.send(res.objetos);
});
CodePudding user response:
As you may be aware cors is used to enable cross origin resource sharing. I'm assuming your error is coming from trying to trigger to OnRequest
from other origins.
You can do the following just below your imports:
const cors = require('cors')({
origin: true //this will allow all origins, you can limit to to a particular domain etc. but this is a good option for a public api.
});
Then try the following:
export trkFun = functions.https.onRequest(
async (request, response) => {
cors(request, response, async () => {
//... do your things in here
}
});