Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase cloud function onCall not working after changing region

I built few cloud functions like this one:

const addRoom = functions.https.onCall((data, context) => {

It works perfectly but I wanted to change region to europe-west. I followed this stackoverflow: firebase deploy to custom region (eu-central1)

const addRoom = functions.region('europe-west1').https.onCall((data, context) => {

It looks working fine for all functions (triggers) except onCall functions. I got this error when calling addRoom function on client side :

firebase.functions().httpsCallable("addRoom")(data)

Access to fetch at 'https://us-central1-myproject.cloudfunctions.net/addRoom' from origin 'http://localhost:4200/new-room' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

At this moment I use default region for onCall functions but is there a way to correct that or is that an error from firebase ?

like image 935
Emerica Avatar asked Jan 27 '19 15:01

Emerica


People also ask

How do I change the region of a cloud function?

You can select a region for your function during deployment. If you are using the Google Cloud CLI, you can specify the region by using the --region flag: gcloud functions deploy FUNCTION_NAME --region= REGION ... Where REGION is one of the regions listed above.

What is the difference between onCall http callable and onRequest HTTP request functions?

onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.

How many requests can handle a single cloud function?

By default each Cloud Run container instance can receive up to 80 requests at the same time; you can increase this to a maximum of 1000.


1 Answers

On the client side, you should specify the desired region at initialization and call the function as follows:

var functions = firebase.app().functions('europe-west1');

....

functions.httpsCallable("addRoom")(data)

See https://firebase.google.com/docs/functions/locations#http_and_client_callable_functions

like image 195
Renaud Tarnec Avatar answered Nov 23 '22 00:11

Renaud Tarnec