javascript firebase cloud函数:如何修复require未定义错误



我正试图使用它们的教程,但我不断得到错误要求是没有定义的。我知道require主要用于nodejs服务器端应用程序,但如何在客户端解决这个问题。必须有一种方法,如果firebase教程指定了它。

未捕获引用错误:未定义要求

这是我的main.js文件

const firebase = require("firebase");
// Required for side-effects
require("firebase/functions");


checkout()
function checkout(){

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)
// ...
})
}
main.html file
<!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-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>
<script type="text/javascript">
<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>

</script>

<script src="main.js"></script> 
</body>
</html>

如何修复未定义require并从客户端调用firebase函数的错误?

由于您已经将Firebase脚本添加到HTML中,因此不需要使用require来再次添加它们。除此之外,require仅适用于nodejs或模块捆绑器等环境,而您在这里没有使用这些环境。

只需删除需求,即可开始使用已添加的Firedrops SDK。

相关内容

最新更新