vendredi 2 avril 2021

What is problem in my code I am trying to retrive data from firebase database using javascript

What is the problem in my code I am trying to retrieve data from the firebase database using javascript? But not retrieving any data but when I save any data that time it saves successfully in dayabse.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="form_container">
        <input placeholder="Enter Number Of Users Present" type="number" id="number"/>
    </div>

    <div id="form_container1">
        <input placeholder="Enter Todays Date" type="date" id="todaydate"/>
    </div>

    <div id="button_container">
    <button id="savedata" onclick="save()"> Save </button>
    </div>

    <div id="button_container3">
    <button id="read" onclick="get()"> Read </button>
    </div>
</body>
</html>

index.js

    function save() 
   {
        var data = document.getElementById('number').value
        var date = document.getElementById('todaydate').value

        database.ref('users/' + date).set({
        data : data,
        date : date
        })

        alert('Saved')

    }
    function get() {
        var username = document.getElementById('todaydate').value
      
        var user_ref = database.ref('users/' + date)
        user_ref.on('value', function(snapshot) {
          var data = snapshot.val()
          document.getElementById('number').value = snapshot.val().data;
      
        })
      }

My database main file name is users and the subfile name is data. Can anyone please tell me what is problem with my code??




Aucun commentaire:

Enregistrer un commentaire