How do I update data in indexedDB?

后端 未结 4 947
情书的邮戳
情书的邮戳 2021-02-07 20:48

I have tried to get some information from W3C regarding the update of an objectStore item in a indexedDB database, but with not so much susccess. I found here a way to do it, bu

4条回答
  •  离开以前
    2021-02-07 21:48

    this is case of update infos of an user object

    var transaction = db.transaction(["tab_user"], "readwrite");
    var store = transaction.objectStore("tab_user");
    
    var req = store.openCursor();
    req.onerror = function(event) {
      console.log("case if have an error");
    };
    
    req.onsuccess = function(event) {
        var cursor = event.target.result;
        if(cursor){
            if(cursor.value.idUser == users.idUser){//we find by id an user we want to update
                var user = {};
                user.idUser = users.idUser ;
                user.nom = users.nom ;
    
                var res = cursor.update(user);
                res.onsuccess = function(e){
                    console.log("update success!!");
                }
                res.onerror = function(e){
                    console.log("update failed!!");
                }
            }
            cursor.continue();
        }
        else{
            console.log("fin mise a jour");
        }
    }
    

提交回复
热议问题