vendredi 28 décembre 2018

How to remove data (with prevChildKey) from Firebase Realtime Database?

I asked a question before here and I keep going through the same code. I can't figure out how to delete items with prevChildKey.

Console error give this :

Uncaught ReferenceError: deleteData is not defined at HTMLButtonElement.onclick (index.html:1)

index.html

 <div id="list_div" class="list-div">
   </div>

index.js

    // Database Reference
       var DataRef = firebase.database().ref('users/' + currentUser.uid + '/exercises/');

    // Read data from database
        function readData(){
          //var readDataRef = firebase.database().ref('users/' + currentUser.uid + '/exercises/');    
          DataRef.on("child_added", function(data, prevChildKey) {
            var newData = data.val();
            console.log("ID: " + prevChildKey);
            console.log("name: " + newData.name);
            console.log("sets: " + newData.sets);
            console.log("reps: " + newData.reps);
            console.log("weights: " + newData.weights);

            document.getElementById("list_div").innerHTML+=`

              <div id="listitem_div" class="listitem-div">
                <span class="listtext">Exercise Name: </span>${newData.name}<br>
                <span class="listtext">Set: </span>${newData.sets}
                <span class="listtext">Reps: </span>${newData.reps}
                <span class="listtext">Weights: </span>${newData.weights}
                <button type="submit" id="itemremove" class="itemremove"  onclick="deleteData(${prevChildKey})">Delete</button>
              </div>
            `
          });
        }

    //Delete data from database
        function deleteData(prevChildKey){
            var newDataRef = firebase.database().ref('users/' + currentUser.uid + 'exercises/' + prevChildKey);
              newDataRef.remove()
        }




Aucun commentaire:

Enregistrer un commentaire