I’m trying to see whether an entry already exists in an objectStore. If the entry exists, I need to alter a few of its fields; if the entry does not exist, I need to add it.
Here’s my query:
var db = html5rocks.indexedDB.db;
var trans = db.transaction(["topics"], "readwrite");
var store = trans.objectStore("topics");
var findRequest = store.get(tHash);
findRequest.onsuccess = function(event){
console.log("logging event");
console.log(event);
var cursor = event.target.result;
}
findRequest.onerror(e){
console.log(e);
}
The issue I’m experiencing is that the findRequest.onsuccess function always fires – even though it’s impossible that the get(tHash) request is finding anything (By the way, the “topics” objectStore contains zero entries when I do this…).
I figured I’d use the onsuccess and onerror functions to tell if I needed to update an existing record or create a new one. What am I doing wrong?
Directly from the horse’s mouth:
I think you want something like: