rID133
rID133

Reputation: 163

Firebase callable functions, unsure about client code, getting internal error

I have been trying to get firebase functions to work for a day but failing miserably.

I have set up a http cloud function from the google cloud functions inline editor with code:

const admin = require('firebase-admin');
const functions = require('firebase-functions')

admin.initializeApp();
const db = admin.firestore();

exports.account_create_callable = functions.https.onCall((data,context) => {
   var docRef = db.collection('users').doc(context.auth.uid)
   docRef.set(
    {
      createdAt:Date.now(),
    }, { merge: true }
   );
   return
 })

I'm using React and there is very limited documentation but I've put together what I think is correct:

import firebase from "firebase/app";
import "firebase/auth";
import "firebase/firestore";
import "firebase/functions";

const firebase = firebase.initializeApp({
   apiKey: process.env.REACT_APP_FB_API_KEY,
   authDomain: process.env.REACT_APP_FB_AUTH_DOMAIN,
   projectId: process.env.REACT_APP_FB_PROJECT_ID,
});


const firestore = firebase.firestore();
// require('firebase-functions'); // I saw this is some documentation but don't think it's needed?
const functions = firebase.functions()

export function createUser(uid, data) {
   const account_create_callable = functions().httpsCallable('account_create_callable')
   account_create_callable()
}

However when I run this code, I get an internal error shown in the browser console.

Error: internal
at new HttpsErrorImpl (error.ts:65)
at _errorForResponse (error.ts:175)
at Service.<anonymous> (service.ts:276)
at step (tslib.es6.js:100)
at Object.next (tslib.es6.js:81)
at fulfilled (tslib.es6.js:71)

Interestingly, I get the same error when I call a non existent function like so:

functions().httpsCallable('asdf') 

Would appreciate if someone could let me know where I'm going wrong. My Firebase SDK is up to date. I set up the function in the inline editor and set my region to eu-west 2, would this have any effect on my client code?

Upvotes: 0

Views: 555

Answers (1)

Daniel L
Daniel L

Reputation: 540

Since your Callable Function is deployed on europe-west2, you must initialize your client SDK with the appropriate region:

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

See https://firebase.google.com/docs/functions/locations#client-side_location_selection_for_callable_functions.

Upvotes: 2

Related Questions