首页 > 解决方案 > 尝试初始化时出现 JS 'Firebase is not defined' 错误

问题描述

我正在与 Glitch.com 合作进行我的第一个物联网项目(也没有前端/后端经验)。我设置了这个水分植物传感器并在 Arduino 中添加了一些代码。水分从 Arduino 上传到 Firebase 实时数据库。

现在,当尝试将 Firebase 中的这些数据添加到我的网页上时,我一直遇到同样的错误。在我自己多次尝试失败后,我决定只为 Firebase 连接重新混合现有项目。填写完我自己的所有 Firebase 信息(身份验证、url ...)后,我仍然遇到了同样的问题,“未定义 Firebase”。此错误发生在这 3 条规则上;

firebase.initializeApp(config);
var rootRef = firebase.database().ref();
var myDBConn = firebase.database().ref("Moisture");

完整代码(对于某些上下文):

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

    // Initialize Firebase
    // TODO: Replace with your project's customized code snippet
  var config = {
        apiKey: "your apiKey from firebase website",
        authDomain: "projectId.firebaseapp.com",
        databaseURL: "https://databaseName.firebaseio.com",
        storageBucket: "bucket.appspot.com",
      };
        firebase.initializeApp(config);

        var rootRef = firebase.database().ref();


  // List to hold my moisture value
  var myMoisture = [];

  // Define database connection to correct branch, Moisture
  var myDBConn = firebase.database().ref("Moisture");

  // Function that acts when a 'new child is added to the DB' - i.e. new data is added this function runs.
  myDBConn.on("child_added", function(data, prevChildKey){

    // The data returned from the branch is put into a variable, dataPoint
    var dataPoint = data.val();

    // Convert the 'Temp' field from dataPoint into integers, then put them into the myTemps list
    myMoisture.push(parseInt(dataPoint.Temp));

    // Add all the Temps and get the total
    var totalT = 0;
    var i=0;
    for (i=0; i<myMoisture.length; i++){
      totalT += myMoisture[i];
    }

    // Create an average by dividing the sum of temps by the number of temps
    var average = totalT / myMoisture.length;

    // Update the page elements with the average and the last item (most recent) off the list 
    document.getElementById("averageT").innerHTML=average;
    document.getElementById("LiveT").innerHTML=myMoisture[myMoisture.length - 1];
  });

我想知道的另一件事是, datapoint.Temp 是什么意思?原始代码是为显示实时温度和平均温度的网页制作的,但是我想要湿度值。我仍然需要稍微编辑一下代码,但要在开始之前确保 Firebase 连接正常工作。

标签: javascriptfirebase

解决方案


您必须在script标签中包含 JavaScript 代码,如下所示:

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

<script>
    // Initialize Firebase
    // TODO: Replace with your project's customized code snippet
  var config = {
        apiKey: "your apiKey from firebase website",
        authDomain: "projectId.firebaseapp.com",
        databaseURL: "https://databaseName.firebaseio.com",
        storageBucket: "bucket.appspot.com",
      };
        firebase.initializeApp(config);

        var rootRef = firebase.database().ref();


  // List to hold my moisture value
  var myMoisture = [];

  // Define database connection to correct branch, Moisture
  var myDBConn = firebase.database().ref("Moisture");

  // Function that acts when a 'new child is added to the DB' - i.e. new data is added this function runs.
  myDBConn.on("child_added", function(data, prevChildKey){

    // The data returned from the branch is put into a variable, dataPoint
    var dataPoint = data.val();

    // Convert the 'Temp' field from dataPoint into integers, then put them into the myTemps list
    myMoisture.push(parseInt(dataPoint.Temp));

    // Add all the Temps and get the total
    var totalT = 0;
    var i=0;
    for (i=0; i<myMoisture.length; i++){
      totalT += myMoisture[i];
    }

    // Create an average by dividing the sum of temps by the number of temps
    var average = totalT / myMoisture.length;

    // Update the page elements with the average and the last item (most recent) off the list 
    document.getElementById("averageT").innerHTML=average;
    document.getElementById("LiveT").innerHTML=myMoisture[myMoisture.length - 1];
  });

</script>

有关更多 JavaScript 详细信息,请参阅https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics和/或https://developer.mozilla.org/en-US/docs/Web/JavaScript/指导


推荐阅读