Invoking CloudFunctions from Web Application resulting in CORS

2,606 views
Skip to first unread message

Dalton Fury

unread,
May 3, 2020, 11:24:44 AM5/3/20
to Firebase Google Group
Firebase is "randomly" giving CORS issue when connecting to cloud functions from a Web App.

I have functions defined like this:

const functions = require("firebase-functions");
const admin = require("firebase-admin");


admin
.initializeApp();


exports
.createQueue = functions.https.onCall((data, context) => {
   
...
});


and I am calling the function from a web client like this:

        firebase.initializeApp(firebaseConfig);
       
this.queues = firebase.firestore().collection("queuesFromFBFn");


        firebase
.auth().signInAnonymously().catch(error => console.error(error));
       
this.functions = firebase.app().functions();


       
const createQueueFn = this.functions.httpsCallable('createQueue');
       
return createQueueFn({
            name
: name,
       
}).then(response => response.data.data)


Invoking the function is giving CORS errors randomly. They go away for a while if I remove the cloud function and reupload it by changing just the function name, suggesting that the problem is not with the code. The function is working fine when ran locally using the emulator too. The function is Invokable by allUsers in the GCP console.

Screenshot 2020-05-03 at 8.03.17 PM.png



I have been trying to fix it for the last four days. Is it becasue of some misconfiguration which I can't get my head around?

Michael Bleigh

unread,
May 3, 2020, 11:35:04 AM5/3/20
to Firebase Google Group
You might get CORS errors if the function crashes in am unexpected way. Do you see anything in the function logs when you get these kinds of errors?

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/3e73b8f4-2e1a-4045-9b11-b4f4c5751d05%40googlegroups.com.

Dalton Fury

unread,
May 3, 2020, 5:40:26 PM5/3/20
to fireba...@googlegroups.com
Hey Michael,

THe logs are clean, I put a console.log right at line 1, it's not running.

The issue goes away on deleting the function and redeploying it, also it goes away naturally after some time, hence I am suspecting that it's because of something else.

Yes, I figured out that if the function crashes, it gives a CORS error, but it might not be the case as the function with the same input works locally on the firebase shell. 

I have anonymous authentication enabled. Is it because of that, for some reason?

Thanks,
Fury


Emiliano Hernandez

unread,
May 4, 2020, 8:18:52 AM5/4/20
to Firebase Google Group
One thing you can do, that has worked for me:

1. Lint the function with eslint..
2. Check the function name is the same one you are calling, if you are calling a non existing function you get CORS. (This is where 99% of my CORS errors come from)

If you are sure firebase is doing something wrong delete the function from the console and redeploy.
Reply all
Reply to author
Forward
0 new messages