How to update data in indexedDB?

I tried to get some information from the W3C regarding updating the objectStore object in the indexedDB database, but with not so much susccess. I found a way here to do this, but actually it does not work for me.

My implementation is something like this

DBM.activitati.edit = function(id, obj, callback){ var transaction = DBM.db.transaction(["activitati"], IDBTransaction.READ_WRITE); var objectStore = transaction.objectStore("activitati"); var keyRange = IDBKeyRange.only(id); objCursor = objectStore.openCursor(keyRange); objCursor.onsuccess = function(e){ var cursor = e.target.result; console.log(obj); var request = cursor.update(obj); request.onsuccess = function(){ callback(); } request.onerror = function(e){ conosole.log("DBM.activitati.edit -> error " + e); } } objCursor.onerror = function(e){ conosole.log("DBM.activitati.edit -> error " + e); } } 

I have all the DBM.activitati methods. (add | remove | getAll | getById | getByIndex), but I cannot resolve this.

If you know how I can do this, please tell us!

Thanks!

+6
source share
4 answers

Check out this jsfiddle for some examples on how to update IDB entries. I worked on this with another StackOverflower - this is a pretty decent standalone IndexedDB example that uses indexes and updates.

The method you seem to be looking for is put , which either inserts or updates the record if there are unique indexes. In this sample script, it is used as follows:

  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 have some possible syntax errors, typo "console" in console.log as "conosole".

+14
source

A bit late for an answer, but perhaps this helps others. I still stumbled, as I think, on the same issue, but it is very simple:

If you want INSERT or UPDATE records, you use objectStore.put(object) (help)
If you want only INSERT records, you use objectStore.add(object) (help)

So, if you use add(object) , and the record key still exists in the database, it will not be overwritten and will throw error 0 "ConstraintError: Key already exists in the object store."

If you use put(object) , it will be overwritten.

+2
source

this is a case of user object update information

 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"); } } 
+1
source

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 :) } 
+1
source

Source: https://habr.com/ru/post/919079/


All Articles