我一直在修改旧项目并进行一些改进,而我似乎无法弄清楚如何从indexedDB加载单个条目。我已经清除了数据库并完成了新的导入,我可以在chrome检查器资源部分中看到所有记录(包括我用于entryID的记录)。我试过拉各种ID号码,我可以确认它们都在检查员的数据库中,它们都返回未定义。这是我正在使用的代码。
/*
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);
};
}
}
这实际上只是原始版本的一些略微修改的代码(因为这个功能是相同的,我真的只修改了这里和导入器中的数据库和ObjectStore名称)。在Chrome中运行此代码(在我知道所有其他数据库相关功能完成后手动触发)会触发&#34; onsuccess&#34;甚至,只是有一个未定义的结果(好像条目不在数据库中,但我再次检查它在那里)。
根据要求,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"
}
创建objectStore(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});
}
};
答案 0 :(得分:14)
发现问题,希望没有人犯下我犯过的同样简单的错误,但万一有人这样做并在这里遇到问题是什么问题。
我在数据库中添加的密钥是一个字符串。我要求的关键是一个int。
不要跟我一样,检查一下你的数据类型。