繁体   English   中英

Javascript:从客户端调用 firebase 函数不起作用

[英]Javascript: calling firebase function from client side is not working

我试图从客户端调用 firebase 函数,但我收到了 firebase.functions 不是函数的错误

main.js:18 Uncaught TypeError: firebase.functions 不是函数

这是 main.html 代码

 <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body > <div class="jumbotron text-center"> <h1>My First Bootstrap Page</h1> <p>Resize this responsive page to see the effect!</p> </div> <script src="https://www.gstatic.com/firebasejs/8.0.2/firebase.js"></script> <script src="https://www.gstatic.com/firebasejs/8.0.2/firebase-functions.js"></script> <!-- The core Firebase JS SDK is always required and must be listed first --> <script src="https://www.gstatic.com/firebasejs/8.0.2/firebase-app.js"></script> <!-- TODO: Add SDKs for Firebase products that you want to use https://firebase.google.com/docs/web/setup#available-libraries --> <script src="https://www.gstatic.com/firebasejs/8.0.2/firebase-analytics.js"></script> <script type="https://requirejs.org/docs/release/2.3.5/minified/require.js"></script> <script type="text/javascript"> var firebaseConfig = { apiKey: "41414414I", authDomain: "414444.firebaseapp.com", databaseURL: "https://414144.firebaseio.com", projectId: "414-c4cc", storageBucket: "4144-cc4cc.appspot.com", messagingSenderId: "414542", appId: "1:41:web:31415425", measurementId: "G-542g542" }; // Initialize Firebase firebase.initializeApp(firebaseConfig); firebase.analytics(); // Initialize Cloud Functions through Firebase var functions = firebase.functions(); </script> <script> // Your web app's Firebase configuration // For Firebase JS SDK v7.20.0 and later, measurementId is optional </script> <script src="main.js"></script> </body> </html>

这是我的 main.js 文件

 random() function random(){ var randomNumber = firebase.functions().httpsCallable('randomNumber'); randomNumber({text: messageText}).then(function(result) { // Read result of the Cloud Function. var sanitizedMessage = result.data.text; console.log(sanitizedMessage) }).catch(function(error) { // Getting the Error details. var code = error.code; var message = error.message; var details = error.details; console.log(message) // ... }) }

如何修复错误并从客户端调用 firebase 函数?

实施道格的答案后,它显示

未捕获的 ReferenceError:需要未定义

您的 Firebase 脚本包含不正确且乱序:

<script src="https://www.gstatic.com/firebasejs/8.0.2/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.0.2/firebase-functions.js"></script>

<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.0.2/firebase-app.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/8.0.2/firebase-analytics.js"></script>

请查看文档中的说明 首先,不要包含 firebase.js。 其次,将它们按正确的顺序排列,首先列出 firebase-app。

<script src="https://www.gstatic.com/firebasejs/8.0.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.0.2/firebase-functions.js"</script>
<script src="https://www.gstatic.com/firebasejs/8.0.2/firebase-analytics.js"</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM