Home > Software design >  Using indexedDB, I have a event listener which calls a function when a div is clicked, but it doesn&
Using indexedDB, I have a event listener which calls a function when a div is clicked, but it doesn&

Time:05-09

I have this function:

function event_erase_nota (){
       
    document.addEventListener('click', (e)=> {
    let elementParent = e.target.parentNode;
        
        if (elementParent.className == 'delete'){
            id = elementParent.parentNode.id;
            
            console.log(id) 
            erase_nota(id)
       }
    })
}
event_erase_nota()

Detect when a div with the class 'delete' is clicked and call the function erase_nota that is on other archive.


function erase_nota (id) {
    const request = indexedDB.open("noto", 1);
    
    request.onsuccess = (event) => {
        var db = event.target.result;
        
        let trx = db.transaction("nota", "readwrite");
        let request = trx.objectStore("nota").delete(id);
        
        trx.oncomplete = function () {
            console.log("Nota eliminada")
            
            db.close();
        }
    }
} 

But the note stores in the data base doesn't get erased! When I call the function 'erase_nota(someId)' in the console works fine.

What's wrong?

CodePudding user response:

Try:

let trx = db.transaction(["nota"], "readwrite");

CodePudding user response:

The problem was the type of the variable id:

if (elementParent.className == 'delete'){
            id = elementParent.parentNode.id;
            
            console.log(id) 
            erase_nota(id) // 

I checked the typeof id and was a string, so I convert it to Number,

if (elementParent.className == 'delete'){
            id = elementParent.parentNode.id;
            
            console.log(id) 
            erase_nota(Number(id)) 

Now it works!

  • Related