Emeric
Emeric

Reputation: 6885

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 ?

Upvotes: 8

Views: 3888

Answers (2)

Rabie Daddi
Rabie Daddi

Reputation: 184

for angular application you should provide the region like this in app module

providers: [
    { provide: REGION, useValue: 'europe-west3' }
]

and call the function like this:

constructor(private aff: AngularFireFunctions) {}

sendMail(data: { ... }): Promise<any> {
    return this.aff.httpsCallable('function_name')(data).pipe(first()).toPromise()
}

Upvotes: 0

Renaud Tarnec
Renaud Tarnec

Reputation: 83068

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

Upvotes: 14

Related Questions