Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试在网页上显示实时Firebase db数据时出现Firebase未定义js错误_Javascript_Html_Firebase_Firebase Realtime Database - Fatal编程技术网

Javascript 尝试在网页上显示实时Firebase db数据时出现Firebase未定义js错误

Javascript 尝试在网页上显示实时Firebase db数据时出现Firebase未定义js错误,javascript,html,firebase,firebase-realtime-database,Javascript,Html,Firebase,Firebase Realtime Database,我正在为我的第一个物联网项目使用湿度传感器。我正在将传感器的数据保存在实时Firebase数据库中。对于我的网页,我正在使用glitch.com,并试图在我的网页上显示数据库中的数据。我试着自己编写代码,但几乎一无所获。所以我决定重新混合现有的glitch.com“Firebase Read”项目。然而,我遇到了一个问题,它说:“firebase”没有在以下3行中定义(在下面列出的第一行中,它还说配置没有定义): 我的js文件中当前包含以下内容: <script src="https://

我正在为我的第一个物联网项目使用湿度传感器。我正在将传感器的数据保存在实时Firebase数据库中。对于我的网页,我正在使用glitch.com,并试图在我的网页上显示数据库中的数据。我试着自己编写代码,但几乎一无所获。所以我决定重新混合现有的glitch.com“Firebase Read”项目。然而,我遇到了一个问题,它说:“firebase”没有在以下3行中定义(在下面列出的第一行中,它还说配置没有定义):

我的js文件中当前包含以下内容:

<script src="https://www.gstatic.com/firebasejs/7.11.0/firebase.js"></script> 
    // Initialize Firebase
    // TODO: Replace with your project's customized code snippet
  // Initialize Firebase
  var firebaseConfig = {
    apiKey: "xx",
    authDomain: "xx",
    databaseURL: "xx",
    projectId: "xx",
    storageBucket: "xx",
    messagingSenderId: "xx",
    appId: "xx"
  };
  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];
  });

//初始化Firebase
//TODO:替换为项目的自定义代码段
//初始化Firebase
var firebaseConfig={
apiKey:“xx”,
authDomain:“xx”,
数据库URL:“xx”,
投影:“xx”,
storageBucket:“xx”,
messagingSenderId:“xx”,
appId:“xx”
};
firebase.initializeApp(配置);
var rootRef=firebase.database().ref();
//列表来保存我的水分值
var myweather=[];
//定义数据库连接到正确的分支、连接
var myDBConn=firebase.database().ref(“湿度”);
//“向数据库中添加新的子项”(即添加新数据)时起作用的函数此函数运行。
myDBConn.on(“添加的子对象”,函数(数据,prevChildKey){
//从分支返回的数据被放入变量dataPoint中
var dataPoint=data.val();
//将数据点中的“Temp”字段转换为整数,然后将它们放入myTemps列表中
myweather.push(parseInt(dataPoint.Temp));
//把所有的临时工加起来,得到总数
var totalT=0;
var i=0;
对于(i=0;i

快速扫描FireBase文档

我发现您的FireBase导入有点错误:

尝试将其更改为:

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


文档在这里:

错误:listen EADDRINUSE:address read in use:::3000表示您尝试使用的端口上已运行某些内容。嗯,很有趣,我将对此进行研究,谢谢!谢谢,我更改了它,遗憾的是它没有解决错误。
  var myDBConn = firebase.database().ref("Moisture");
<script src="https://www.gstatic.com/firebasejs/7.11.0/firebase.js"></script> 
    // Initialize Firebase
    // TODO: Replace with your project's customized code snippet
  // Initialize Firebase
  var firebaseConfig = {
    apiKey: "xx",
    authDomain: "xx",
    databaseURL: "xx",
    projectId: "xx",
    storageBucket: "xx",
    messagingSenderId: "xx",
    appId: "xx"
  };
  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];
  });
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Hello!</title>

    <!-- The core Firebase JS SDK is always required and must be listed first -->
    <script src="https://www.gstatic.com/firebasejs/7.11.0/firebase.js"></script> 
    <script src="script.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">

  </head> 

  <body>

    <h1>Reading from Firebase Cloud Database</h1>

    Live Moisture Reading: <a id="LiveT">---</a> <br>

    Average Temperature: <a id="averageT">---</a>



  </body>
</html>
Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1270:14)
    at listenInCluster (net.js:1318:12)
    at Server.listen (net.js:1405:7)
    at Lws.listen (/opt/nvm/versions/node/v10.15.3/pnpm-global/1/node_modules/.registry.npmjs.org/lws/1.3.2/node_modules/lws/index.js:81:12)
    at WsServe.execute (/opt/nvm/versions/node/v10.15.3/pnpm-global/1/node_modules/.registry.npmjs.org/lws/1.3.2/node_modules/lws/lib/command/serve.js:296:26)
    at WsServe.execute (/opt/nvm/versions/node/v10.15.3/pnpm-global/1/node_modules/.registry.npmjs.org/local-web-server/2.6.0/node_modules/local-web-server/lib/command/serve.js:11:18)
    at Map.start (/opt/nvm/versions/node/v10.15.3/pnpm-global/1/node_modules/.registry.npmjs.org/cli-commands/0.4.0/node_modules/cli-commands/index.js:26:18)
    at WsCliApp.start (/opt/nvm/versions/node/v10.15.3/pnpm-global/1/node_modules/.registry.npmjs.org/lws/1.3.2/node_modules/lws/lib/cli-app.js:9:26)
    at Function.run (/opt/nvm/versions/node/v10.15.3/pnpm-global/1/node_modules/.registry.npmjs.org/lws/1.3.2/node_modules/lws/lib/cli-app.js:15:29)
    at Object.<anonymous> (/opt/nvm/versions/node/v10.15.3/pnpm-global/1/node_modules/.registry.npmjs.org/local-web-server/2.6.0/node_modules/local-web-server/bin/cli.js:5:29)
  <!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
  <script src="https://www.gstatic.com/firebasejs/7.11.0/firebase-app.js"></script>