为 Cloud Functions 指定区域时出现异常“firebase.functions() requires ... no argument ...”

Ren*_*nec 8 firebase google-cloud-functions

我正在关注 Firebase文档,以便从网页调用位于与us-central1.

更准确地说,在网页中,我做了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).
Run Code Online (Sandbox Code Playgroud)

这是MCV代码:

云功能

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

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

网页

<!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>
Run Code Online (Sandbox Code Playgroud)

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

云函数(us-central1版本)

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

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

网页(us-central1版本)

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

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

        var functions = firebase.functions();

        ...    

  </script>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

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

Gio*_*lis 6

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

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');
Run Code Online (Sandbox Code Playgroud)

  • @DougStevenson 如果可以的话,文档似乎是错误的或不完整的。查看问题和实现文档中所述内容的 MCV 代码,即 `var functions = firebase.functions('europe-west1');` 并给出错误。 (6认同)
  • 我要说的是,如果你执行 `firebase.initializeApp(config); var functions = firebase.functions('xxxx');` 它不起作用。如果根据回复修改文档可能会更好,因为我们至少有两个人会犯这个错误。 (4认同)