为云函数指定区域时,异常“ firebase.functions()...没有参数...”

时间:2018-08-10 13:00:19

标签: firebase google-cloud-functions

我正在跟踪Firebase documentation,以便从网页上调用与us-central1所在区域不同的Callable Function。

更确切地说,在网页中,我做了var functions = firebase.functions('europe-west1');,但收到以下错误,但通话没有结果:

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

这是MCV代码:

云功能

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

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

网页

<!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>

如果我在us-central1区域中运行相同的代码(不带参数调用firebase.functions()),它将正常工作。

云功能(us-central1版本)

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

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

网页(us-central1版)

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

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

        var functions = firebase.functions();

        ...    

  </script>

</body>
</html>

请注意,我已经将开发环境更新为firebase-functionsfirebase-adminfirebase-tools的最新版本。

2 个答案:

答案 0 :(得分:4)

通过反复试验,我发现您必须这样做:

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');

答案 1 :(得分:0)

做到这一点的官方方法是:

var functions = firebase.app().functions('europe-west1');
如文档https://firebase.google.com/docs/functions/locations

中所述