4

server code

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.hello = functions.https.onCall((data, context) => {
  console.log(data.text);
  return 123;
});

client code

const myFunc = firebase.functions().httpsCallable('hello');
myFunc({text: 'world'}).then((rslt)=>{
  console.log('rslt:', rslt);
});

But it is not work. I got error message at client browser console.

Access to fetch at 'https:// ==skip== /hello' from origin 'http://localhost:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

How can I solve this problem? I did not find a solution in the google guide documentation.

I see comments and add content.

The query does not arrive at the server.

If I try after functions deploy it works fine.

But it doesn't show up on the local console, but on the firebase console.

How can I make a local server call?

It's crazy to deploy every time you test a function.

Barak
  • 1,390
  • 15
  • 27
msm082919
  • 617
  • 8
  • 24

1 Answers1

1

Hard to tell from the given code why your request violates Cross-Origin Resource Sharing. Take a read on this matter and figure out what is it that you include (whether by intention or not) in your HTTP request that causes it. Headers maybe?

In the meantime, you can enable CORS in Firebase Functions with require("cors"):

const cors = require("cors")({origin: true});
const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

exports.hello = functions.https.onCall((data, context) => {
  cors(data, context, () => {
     console.log(data.text);
     res.send("123");
  }
});

Regarding local testing of Firebase Functions, you can do so by using this command:

firebase serve --only functions


Local testing of Firebase Hosting / Functions

Enabling CORS in Firebase Functions.

Barak
  • 1,390
  • 15
  • 27