How do I update data in indexedDB?

后端 未结 4 952
情书的邮戳
情书的邮戳 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:25

    Check out this jsfiddle for some examples on how to update IDB records. I worked on that with another StackOverflower -- it's a pretty decent standalone example of IndexedDB that uses indexes and does updates.

    The method you seem to be looking for is put, which will either insert or update a record if there are unique indexes. In that example fiddle, it's used like this:

        phodaDB.indexedDB.addUser = function(userObject){
            //console.log('adding entry: '+entryTxt);
            var db = phodaDB.indexedDB.db;
            var trans = db.transaction(["userData"],IDBTransaction.READ_WRITE);
            var store = trans.objectStore("userData");
            var request = store.put(userObject);
    
            request.onsuccess = function(e){
                phodaDB.indexedDB.getAllEntries();
            };
            request.onerror = function(e){
                console.log('Error adding: '+e);
            };
        };
    

    For what it's worth, you've got some possible syntax errors, misspelling "console" in console.log as "conosole".

提交回复
热议问题