I've been reworking an older project and making some improvements and I can no longer seem to figure out how to load a single entry from the indexedDB. I have cleared the DB and done a fresh import and I can see all the records (including the one I am using for the entryID) in the chrome inspectors Resource section. I have tried pulling all sorts of ID numbers, all of which I can confirm are in the DB from the inspector, they all return undefined. This is the code I'm using.
/*
This loads up a specific entry and fills in the #entry-details div with it's data. This div is placed overtop the search
content so that when it is closed the user remains exactly where they were and the search does not need to re-process.
*/
function getEntryDetails(){
var entryID = 193; // just for now, this will be grabbed from somewhere else later
// Where going to need a varible to store all this generated HTML in
var html = '';
// First we need to load up the indexedDB and get the record in question.
var db = indexedDB.open('pediaCache');
// lets get a couple of the errors out of the way.
db.onerror=function(e){html += 'There was an error loading the database.<br/>'+e;}
db.onblocked=function(e){html += 'Database access blocked.<br/>'+e;}
// Now for when things go the right way
db.onsuccess=function(e){
var db = e.target.result;
// Get the requested entry
console.log('Attempting to load entry ID '+entryID);
var transaction = db.transaction(['entries'],'readonly');
var objectStore = transaction.objectStore('entries');
var entry = objectStore.get(entryID);
entry.onerror = function(e) {
console.log('error');
console.log(e.target.result);
console.log(e);
};
entry.onsuccess = function(e) {
console.log('success');
console.log(e.target.result);
console.log(e);
};
}
}
This is really just some slightly modified code from the original version (since this functionality is the same, I really only modified the database and ObjectStore names both here and in the importer). Running this code in Chrome (triggered manually after I know all other DB related functions are done) does fire the "onsuccess" even, just with an undefined result (as if the entry was not in the DB, but again I have checked it's in there).
As requested, the contents of console.dir(e):
{
"path": {
"length": 0
},
"cancelBubble": false,
"returnValue": true,
"srcElement": {
"readyState": "done",
"transaction": {
"onerror": null,
"oncomplete": null,
"onabort": null,
"error": null,
"db": {
"onversionchange": null,
"onerror": null,
"onclose": null,
"onabort": null,
"objectStoreNames": {
"0": "entries",
"length": 1
},
"version": 3,
"name": "pediaCache"
},
"mode": "readonly"
},
"source": {
"autoIncrement": false,
"transaction": {
"onerror": null,
"oncomplete": null,
"onabort": null,
"error": null,
"db": {
"onversionchange": null,
"onerror": null,
"onclose": null,
"onabort": null,
"objectStoreNames": {
"0": "entries",
"length": 1
},
"version": 3,
"name": "pediaCache"
},
"mode": "readonly"
},
"indexNames": {
"0": "title",
"length": 1
},
"keyPath": null,
"name": "entries"
},
"error": null
},
"defaultPrevented": false,
"timeStamp": 1420434102528,
"cancelable": false,
"bubbles": false,
"eventPhase": 0,
"currentTarget": null,
"target": {
"readyState": "done",
"transaction": {
"onerror": null,
"oncomplete": null,
"onabort": null,
"error": null,
"db": {
"onversionchange": null,
"onerror": null,
"onclose": null,
"onabort": null,
"objectStoreNames": {
"0": "entries",
"length": 1
},
"version": 3,
"name": "pediaCache"
},
"mode": "readonly"
},
"source": {
"autoIncrement": false,
"transaction": {
"onerror": null,
"oncomplete": null,
"onabort": null,
"error": null,
"db": {
"onversionchange": null,
"onerror": null,
"onclose": null,
"onabort": null,
"objectStoreNames": {
"0": "entries",
"length": 1
},
"version": 3,
"name": "pediaCache"
},
"mode": "readonly"
},
"indexNames": {
"0": "title",
"length": 1
},
"keyPath": null,
"name": "entries"
},
"error": null
},
"type": "success"
}
And the objectStore creation (onupgradeneeded).
// We need to be able to update the db schema (or create it for that matter)
db.onupgradeneeded=function(e){
var db = e.target.result;
// In the future some of theme might want to get commented out...
postMessage({'status':'importing','message':'Upgrading local database.'});
// Check if the table is in there, if it's not then create it
console.log(db.objectStoreNames);
if(db.objectStoreNames.contains('entries')==false){
var db = db.createObjectStore('entries');
// Create the indexes so we can more easily search and sort and stuff (just one, we sort by name, everything else done by magic)
db.createIndex('title' ,'ZTITLE' ,{unique:false});
}
};
To update an existing data in the database, the put(item, key) method is used. However, if the requested data doesn't exist, this method creates it and inserts it in the Object Store. This method returns the key of the stored object as a result.
Complete HTML/CSS Course 2022The indexeddb is a new HTML5 concept to store the data inside user's browser. indexeddb is more power than local storage and useful for applications that requires to store large amount of the data. These applications can run more efficiency and load faster.
IndexedDB is one of the storage capabilities introduced into browsers over the years. It's a key/value store (a noSQL database) considered to be the definitive solution for storing data in browsers.
Found the problem, hopefully nobody make the same simple mistake I did but in case someone does and runs across this question here's what the problem was.
The key I was adding into the database was a string. The key I was requesting was an int.
Don't be like me, check your data types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With