我正在关注 Firebase文档 https://firebase.google.com/docs/functions/locations#http_and_client_callable_functions为了从网页调用位于不同区域的可调用函数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).
这是 MCV 代码:
云功能
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>
如果我在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-functions
, firebase-admin
and firebase-tools
.