0

I am working on an application for android that lets people login and add their data for an amusement park and I also need an app to view the data stored in the database (such as name, birthdate, children etc) on a PC. I have tried to write a web app but I have a problem connecting it to the database. I am new to databases so it might be from my code, can someone help me? The app only needs to show me the database all the time and update when a person registers.

Currently, I am getting this error:

Uncaught TypeError: firebase.database is not a function
    at index.html:33
    at index.html:66

Here is my index.html file:

<html>
  <head>
    <meta charset="utf-8"/>
    <script src="https://www.gstatic.com/firebasejs/7.6.1/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.6.1/firebase-analytics.js"></script>

    <title>Freaky Firebase Example</title>
  </head>
  <body>
    <h1>Freaky Firebase Realtime Database Example</h1>

    <pre id='object'></pre>


    <script>
      (function(){


        // Initialize Firebase
        var firebaseConfig = {
          apiKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
          authDomain: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX.com",
          databaseURL: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX.com",
          projectId: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
          storageBucket: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX.com",
          messagingSenderId: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
          appId: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
          measurementId: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        };
        firebase.initializeApp(firebaseConfig);
        firebase.analytics();

        var userDataRef = firebase.database().ref("UserData").orderByKey();
        userDataRef.once("value").then(function(snapshot) {
          snapshot.forEach(function(childSnapshot) {
            var key = childSnapshot.key;
            var childData = childSnapshot.val();              

            var name_val = childSnapshot.val().Name;
            var id_val = childSnapshot.val().AssignedID;

            $("#name").append(name_val);
            $("#id").append(id_val);

          });
        });

        /* Data Event Listeners Start */
        const preObject = document.getElementById('object');
        const dbRefObject = firebase.database().ref().child('object');

        dbRefObject.on('value', snap => {

          console.log(snap.val()); 
          preObject.innerText = JSON.stringify(snap.val(), null, 3);

        }, function(error) {
          // The fetch failed.
          console.error(error);
        });


        /* Stop */


      }());
    </script>


  </body>
</html>
samthecodingman
  • 23,122
  • 4
  • 30
  • 54

1 Answers1

1

Along with firebase-app and firebase-analytics, you must import the firebase-database service.

<script src="https://www.gstatic.com/firebasejs/7.6.1/firebase-database.js"></script>
samthecodingman
  • 23,122
  • 4
  • 30
  • 54