我的网站无法显示Firebase实时数据库数据

时间:2019-07-01 07:55:54

标签: html firebase firebase-realtime-database

我已经按照youtube的方式将我的网站与我在firebase中创建的数据库连接起来,但是它无法正常工作。

下面是我的html代码

<html>
<head>
    <script src="index.js"></script>
</head>
<body>
    <img src="banner.jpg"/>
    <h1 id="Result">testing</h1>

    <script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-app.js"></script>
    <script>
    // Your web app's Firebase configuration
    var firebaseConfig = {
        apiKey: "AIxcSyAV9E8ubLcEhJE9MNXFNRTli3vdiLAyqmM",
        authDomain: "python-com-firebase.firebaseapp.com",
        databaseURL: "https://python-com-firebase.firebaseio.com",
        projectId: "python-com-firebase",
        storageBucket: "python-com-firebase.appspot.com",
        messagingSenderId: "624398273473",
        appId: "1:624398273473:web:89b9c4ff9c9d85c3"
    };
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    </script>
</body>
</html>

和index.js的代码

window.alert('test');

var fireResult = document.getByElementById("Result");

var dbRef = firebase.database().ref().child("ParkingSeat");

dbRef.on('value', function(datasnapshot){
    fireResult.innerText = datasnapshot.val();
});

当我进入网站时,我的网站可以运行index.js脚本,警报窗口将弹出。但是数据仍然没有改变。

2 个答案:

答案 0 :(得分:0)

请参见https://firebase.google.com/docs/web/setup#add-sdks-initialize

※单击From the CDN标签。

//.....
<!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-app.js"></script>

<!-- Add Firebase products that you want to use -->
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-database.js">
<script>
//....
</script>

答案 1 :(得分:0)

您必须在Firebase的初始化下方添加index.js脚本,例如此代码

<body>
    <img src="banner.jpg"/>
    <h1 id="Result">testing</h1>

    <script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-app.js"></script>
    <script>
    // Your web app's Firebase configuration
    var firebaseConfig = {
        apiKey: "AIxcSyAV9E8ubLcEhJE9MNXFNRTli3vdiLAyqmM",
        authDomain: "python-com-firebase.firebaseapp.com",
        databaseURL: "https://python-com-firebase.firebaseio.com",
        projectId: "python-com-firebase",
        storageBucket: "python-com-firebase.appspot.com",
        messagingSenderId: "624398273473",
        appId: "1:624398273473:web:89b9c4ff9c9d85c3"
    };
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    </script>
    <script src="index.js"></script>
</body>

您必须从头部删除<script src="index.js"></script>