csb
csb

Reputation: 336

IndexedDB updating a record

Trying to update a records name field but instead of updating the record it creates a new record.

My Code

function editName(e) {
var transaction = db.transaction(["people"],"readwrite");
var store = transaction.objectStore("people");
var request = store.get(5);

request.onsuccess = function(e) {
    var data = e.target.result;
    data.name = "John";

    var objRequest = store.put(data);

    objRequest.onsuccess = function(e){
        console.log('Success in updating record');
    };
}

}

Looking for record 5 and attempting to change it's name field to John. I have been following Raymond Camden tutorial http://code.tutsplus.com/tutorials/working-with-indexeddb-part-2--net-35355 and searched for other examples like IndexedDB update by id but can't seem to adapt them to work for what I need.

Upvotes: 2

Views: 5583

Answers (1)

csb
csb

Reputation: 336

Thanks to Tiffany Brown's article which can be found at https://dev.opera.com/articles/introduction-to-indexeddb/ I found a solution which was to add the key within the put call.

var objRequest = store.put(data, +r);

Josh explains in his answer Indexeddb adds a new value instead of updating an existing value that it might have something to do with keys being in-line or out-of-line depending on weather the object store has a key path or not.

Upvotes: 3

Related Questions