4
votes

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?

4

4 Answers

4
votes

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

2
votes

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.

1
votes

After trying across 2 days a variety of refreshes/clean-ups and stuff with CORS, finally found it working after first deleting the function via Firebase console and then deploying the function.

In my case it seems the deployed version got corrupted somehow. It started yesterday when deploy for functions was getting stuck - it would hang and never exit - even though Firebase Status page said all is well. It lasted all day, and I finally let it go to see if it will work today. I thought it was my code, but the deploy is back to working today.

0
votes

This happened to me a couple days ago. The problem was that when i ran firebase deploythe functions in my src directory were not being compiled. There were typescript errors which stopped it from compiling. You can see if it compiled by checking your lib folder and index.ts inside there I believe.