Iām late a couple of years, but thought it would be nice to add my two cents.
First, check out BakedGoods if you don't want to deal with the complex IndexedDB API.
This is a library that establishes a single interface that can be used to perform storage operations in all native and some non-local client repositories. It also supports the flexibility and capabilities provided to each user. Oh, and this is really supported by yours :).
At the same time, placing one or more data elements in the object storage can be as simple as:
bakedGoods.set({ data: [{key: "key1", value: "value1"}, {key: "key2", value: "value2"}), storageTypes: ["indexedDB"], complete: function(byStorageTypeResultDataObj, byStorageTypeErrorObj){} });
Now, to answer the real question ...
Let's start by aggregating valuable information that is distributed across existing answers:
IDBObjectStore.put()
adds a new record to the repository or updates an existing one
IDBObjectStore.add()
adds a new record to the store
IDBCursor.update()
updates record at current cursor position
As you can see, OP uses the appropriate method to update the record. However, in his / her non-method code, there are a few things that are wrong (at least in relation to the API today). I have identified and adjusted them below:
var cursorRequest = objectStore.openCursor(keyRange); //Correctly define result as request cursorRequest.onsuccess = function(e){ //Correctly set onsuccess for request var objCursor = cursorRequest.result; //Get cursor from request var obj = objCursor.value; //Get value from existing cursor ref console.log(obj); var request = objCursor.update(obj); request.onsuccess = function(){ callback(); } request.onerror = function(e){ console.log("DBM.activitati.edit -> error " + e); //Use "console" to log :) } } cursorRequest.onerror = function(e){ //Correctly set onerror for request console.log("DBM.activitati.edit -> error " + e); //Use "console" to log :) }
source share