Help me with my code I'm trying to delete data from firebase but I'm getting reference error

officialanshumantiwari

Anshuman Tiwari

Posted on November 19, 2022

Help me with my code I'm trying to delete data from firebase but I'm getting reference error
function showNotes(){
    let html = "";

    let notesElm = document.getElementById('notes');

    get(child(dbRef,"Notes")).then((snapshot) => {
        var count = 0
        if (snapshot.exists()) {
            const noteData = snapshot.val()
            for (const item in noteData) {
                if (Object.hasOwnProperty.call(noteData, item)) {
                    const element = noteData[item];
                    html+=`
                    <div class="card mx-2 my-2 noteCard" style="width: 18rem;">
                    <div class="card-body">
                    <h5 class="card-title">${element.title}</h5>
                    <p class="card-text">${element.text}</p>
                    <button id=${count} onclick="deleteElement(${element.title})" class="btn btn-primary">Delete Node</button>
                    </div>
                     </div>
                     `;
                    }
                    count++
                }
                notesElm.innerHTML = html;
            } else {
                console.log("No data available");
            }
        }).catch((error) => {
            console.error(error);
        });
} 
function deleteElement(item){
    remove(ref(db,"Notes/"+item))
    .then(()=>{
        console.log("Data has been removed")
    })
    .catch((err)=>{
        console.log(err);
    })
    window.location.reload()
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
officialanshumantiwari
Anshuman Tiwari

Posted on November 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related