可调用的Firebase功能在单击按钮后无任何反应

问题描述

我是Firebase函数的新手,只是尝试做一个简单的测试-单击一个按钮以调用调用的firebase函数

firebase函数只是一个简单的console.log消息。对于这个项目,我正在使用Angular。

我在下面提供了我的前端和后端代码段。

当我单击按钮时,由于某种原因没有任何反应。我希望能调用firebase函数并在控制台中收到“ hello world”消息。

有人知道我在做什么错吗?任何帮助将不胜感激。

谢谢!

Index.js

exports.test = functions.https.onCall(() => {
    console.log('hello world');
});
test() {
    firebase.functions().httpsCallable('test');
  }
<script src="https://www.gstatic.com/firebasejs/7.17.2/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.17.2/firebase-functions.js"></script>

<script>
    var config = {
        apiKey: 'xxx',authDomain: 'xxx',databaseURL: 'xxx',storageBucket: 'xxx'
    };

    // Initialize Cloud Functions through Firebase
    firebase.initializeApp(config);
    var functions = firebase.functions();
</script>

<button (click)="test()">Test</button>

解决方法

您的代码实际上并未调用该函数。调用httpsCallable()时,您需要捕获其返回值,该返回值是您实际用来调用该函数的HttpsCallable对象。

test() {
    const test = firebase.functions().httpsCallable('test');
    test.call();
}

请注意,test()返回的诺言为您提供了该函数的返回值。

我建议您查看documentation以获得更多信息。