Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Callable Function: Response for preflight in invalid

I have created a Firebase callable function, with a simple text return, but am receiving an error when I call the function, both on local and on my deployed app.

The callable function is a simple function to return some text for now:

exports.getSomeInfo = functions.https.onCall(async (data, context) => {
  return 'some info';
});

In my app I load the function with:

const getSomeInfo = firebase.functions().httpsCallable('getSomeInfo');

And call it in the app with:

getSomeInfo();

This produces an error of:

Failed to load https://us-central1-[project-ID].cloudfunctions.net/getSomeInfo: Response for preflight is invalid (redirect)

This error occurs when calling the function on local using firebase serve and on the deployed app.

Viewing the logs in the Firebase Console shows no logs or errors.

Other issues mention this could be a CORS issue, or an incorrect Firebase config. I've ensured the Firebase config is correct. And tried a few of the CORS solutions, but continue to get the error above.

Using [email protected].

What else could be causing this error?

like image 396
Brett DeWoody Avatar asked Mar 06 '23 06:03

Brett DeWoody


2 Answers

As indicated in the documentation, for an HTTPS Callable function you need to "return data that can be JSON encoded".

So if you do something like the following, it should work.

exports.getSomeInfo = functions.https.onCall((data, context) => {
  return {result: 'some info'};
});

Update: removed the async

like image 77
Renaud Tarnec Avatar answered Apr 08 '23 23:04

Renaud Tarnec


April 2020, I just learned the hard way that callable functions have their module name prepended...

In index.js:

const functions = require('firebase-functions')

// ...
exports.callable = require('./callable')

In callable.js:

const functions = require('firebase-functions');
// ... other stuff
exports.myCloudFunction = functions.https.onCall((data, context) => {
  // ...

The way to call this "myCloudFunction" from a JS client is to use its name, prepended with its module name, like this

const fn = firebase.functions().httpsCallable("callable-myCloudFunction")
fn().then(result => { //...

This is documented nowhere, as far as I have found, and, as others have mentioned, almost any error that occurs prior to actually executing the cloud function ends up mislabeled as a CORS error.

like image 29
danh Avatar answered Apr 08 '23 22:04

danh