7
votes

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 ?

2

2 Answers

13
votes

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

0
votes

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()
}