Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception "firebase.functions() takes ... no argument ..." when specifying a region for a Cloud Function

I am following the Firebase documentation in order to call, from a web page, a Callable Function located in a different region than us-central1.

More precisely, in the web page, I do var functions = firebase.functions('europe-west1'); but I get the following error and no result from the call:

uncaught exception: functions: Firebase: firebase.functions() takes either no argument or a Firebase App instance. (app/invalid-app-argument).

Here is the MCV code:

Cloud Function

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

exports.getName = functions
  .region('europe-west1')
  .https.onCall((data, context) => {
    return { name: 'MyName' };
  });

Web Page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="https://www.gstatic.com/firebasejs/5.3.1/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.3.1/firebase-functions.js"></script>

</head>

<body>
    <script>
        // Initialize Firebase
        var config = {
            apiKey: "xxxxxxxxxx",
            authDomain: "xxxxxxxxxx",
            databaseURL: "xxxxxxxxxx",
            projectId: "xxxxxxxxxx",
            storageBucket: "xxxxxxxxxx",
            messagingSenderId: "xxxxxxxxxx",
        };
        firebase.initializeApp(config);   

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

        var getName = functions.httpsCallable('getName');
        getName().then(function (result) {
            console.log((result.data));
        }).catch(function (error) {
            // Getting the Error details.
            var code = error.code;
            var message = error.message;
            var details = error.details;
        });
    </script>

</body>
</html>

If I run the same code in the us-central1 region (calling firebase.functions() without argument) it works correctly.

Cloud Function (us-central1 version)

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

exports.getName = functions  
  .https.onCall((data, context) => {
    return { name: 'MyName' };
  });

Web Page (us-central1 version)

<!DOCTYPE html>
<html lang="en">
.....

<body>
    <script>
        // Initialize Firebase
        var config = {
             .....
        };
        firebase.initializeApp(config);

        var functions = firebase.functions();

        ...    

  </script>

</body>
</html>

Note that I've updated my dev environment to the latest versions of firebase-functions, firebase-admin and firebase-tools.

like image 640
Renaud Tarnec Avatar asked Aug 10 '18 13:08

Renaud Tarnec


2 Answers

By trial and error I found that you have to do it like this:

var config = {
            apiKey: "xxxxxxxxxx",
            authDomain: "xxxxxxxxxx",
            databaseURL: "xxxxxxxxxx",
            projectId: "xxxxxxxxxx",
            storageBucket: "xxxxxxxxxx",
            messagingSenderId: "xxxxxxxxxx",
        };
var fireApp = firebase.initializeApp(config);
var functions = fireApp.functions('europe-west1');
like image 130
Giorgos Kartalis Avatar answered Oct 26 '22 08:10

Giorgos Kartalis


the official way to do that is:

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

as mentioned in the docs https://firebase.google.com/docs/functions/locations

like image 25
GorvGoyl Avatar answered Oct 26 '22 07:10

GorvGoyl