异常"firebase.functions()接受...无自变量..."为Cloud Function指定区域时 [英] Exception "firebase.functions() takes ... no argument ..." when specifying a region for a Cloud Function

查看:77
本文介绍了异常"firebase.functions()接受...无自变量..."为Cloud Function指定区域时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在跟踪Firebase 文档,以便通过网络进行呼叫页面上的可调用函数"位于与us-central1不同的区域中.

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.

更准确地说,在网页中,我执行了var functions = firebase.functions('europe-west1');,但是收到以下错误,但调用没有结果:

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).

这是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()),它将正常工作.

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

云功能(us-central1版本)

Cloud Function (us-central1 version)

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

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

网页(us-central1版本)

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>

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

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

推荐答案

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

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

这篇关于异常"firebase.functions()接受...无自变量..."为Cloud Function指定区域时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆