我有一个非常简单的网页,我正在尝试使用Firebase进行设置;但由于某种原因它无法正常工作。
这是index.html代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Welcome to Firebase Hosting</title>
<!-- update the version number as needed -->
<script defer src="/__/firebase/7.14.4/firebase-app.js"></script>
<!-- include only the Firebase features as you need -->
<script defer src="/__/firebase/7.14.4/firebase-auth.js"></script>
<script defer src="/__/firebase/7.14.4/firebase-database.js"></script>
......
<style media="screen">
......
</style>
</head>
<body>
This is a firebase test page.
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-analytics.js"></script>
<script>
// App's Firebase configuration
var firebaseConfig = {
apiKey: "A.......A",
authDomain: "myapp-6638jj.firebaseapp.com",
databaseURL: "https://myapp-6638jj.firebaseio.com",
projectId: "myapp-6638jj",
storageBucket: "myapp-6638jj.appspot.com",
messagingSenderId: "3.....6",
appId: "1:....:web:.....",
measurementId: "G-73RX..XES"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
<script>
var dbRef = firebase.database().ref('MyList');
dbRef.set({"field1": "XX-UUU","field2": "yy-EEE"});
</script>
</body>
</html>
此外,出于测试目的,我将数据库访问规则设置为:
{
"rules": {
".read": true,
".write": true
}
}
加载页面后,我希望看到一些数据写入了Realtime数据库,但是它完全是空的。
在Web控制台中,我收到此错误消息:
ReferenceError: firebase is not defined
对于那些愿意说的人: 这个错误已经有答案了,我想指出的是错误消息不是:
ReferenceError: Firebase is not defined
确实有答案,但不能解决我的问题。
在发布这篇文章之前,我当然尝试了上面代码的一些变体,但没有奏效。
有人能看到(或猜出)我做错了什么吗?
答案 0 :(得分:1)
在body标签之后添加以下脚本:
<body>
This is a firebase test page.
<!-- Insert these scripts at the bottom of the HTML, but before you use any Firebase services -->
<!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-analytics.js"></script>
<script>
答案 1 :(得分:1)
如果收到firebase未定义的错误,则可能是脚本拼写错误。我建议您检查脚本。如果脚本位于body标签之间并且未定义defer属性,则可能会起作用。
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js"></script> // ıts must be first
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-database.js"></script>