0

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?

rID133
  • 153
  • 1
  • 2
  • 9
  • 1
    Does this answer your question? [Using non-default region for Cloud Functions](https://stackoverflow.com/a/61182270/3068190) – samthecodingman Jun 01 '21 at 03:30
  • Thanks, I've added that code but an internal error is still being raised – rID133 Jun 01 '21 at 14:05
  • If the error persists, then we need to look at the logs for the cloud function side. An error code of `internal` usually signifies some uncaught/unhandled exception on the server side that wasn't wrapped in a [`HttpsError`](https://firebase.google.com/docs/functions/callable#handle_errors). – samthecodingman Jun 01 '21 at 14:13
  • Actually I just changed the permissions to allow all users and now it seems to be working so it was probably a permissions thing. Would you happen to know why the function doesn't pick up on auth from the context? – rID133 Jun 01 '21 at 14:15
  • 1
    In terms of the `allUsers` permission? See [this thread](https://stackoverflow.com/a/66814095/3068190). – samthecodingman Jun 01 '21 at 14:17
  • Thanks so much, exactly the info I needed – rID133 Jun 01 '21 at 14:20

1 Answers1

2

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.

Daniel L
  • 450
  • 3
  • 6