使用JavaScript将Firebase数据库连接到webapp

时间:2016-11-03 16:01:00

标签: javascript firebase web firebase-realtime-database

我的网站需要一些帮助。我坚持将Firebase数据库连接到该站点。我希望数据库中的信息显示在“长度”,“导演”等之后。这是我在我网站上的代码:

<!doctype html>


<html>

<head> 
    <title>Movie title</title>
    </head>

<body>

    <div>
    <img src="bilder/james.jpg" height="300"/>
    </div>

    <script src="https://www.gstatic.com/firebasejs/3.5.1/firebase.js">     </script>
<script>
  var config = {
    apiKey: "AIzaSyC7UjgICAi58ZytqsMVMqQshXgd3lxsVEw",
    authDomain: "test-62cdd.firebaseapp.com",
    databaseURL: "https://test-62cdd.firebaseio.com",
  };
  firebase.initializeApp(config);
</script>

    <script src="index.js"></script>

         Length: 
         Direcor: 
         Actors: 

    </body>


</html>

我还有一个index.js文件:

var langd = document.getElementById("Length");

var langdref = firebase.database().ref().child("Length");

langdref.on("value", function(datasnapshot){

    langd.innertext = datasnapshot.val();

});

2 个答案:

答案 0 :(得分:0)

首先,你的html中没有id为“Length”的元素。检查浏览器的开发人员工具以获取更多信息。如果需要注意,请查看以下链接的“一次读取数据”部分,以帮助从数据库中获取数据。在熟悉Firebase的同时,我大量使用了console.log()。

https://firebase.google.com/docs/database/web/read-and-write

答案 1 :(得分:0)

您可以在html中添加ID: 例如:

<a href="" id="length">my link
</a>

执行此操作后,您的javascript代码将能够通过id操纵此元素绑定。 这是我在代码中所做的一个简单示例:

var mainEmail= document.getElementById("email");

<input type="email" class="form-control" id="email" placeholder="Enter email">