IndexedDB 无论数据库中的结果如何,get()始终返回undefined

j1dl9f46  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(2)|浏览(198)

我一直在修改一个旧的项目,并做了一些改进,但我似乎再也不知道如何从indexedDB中加载单个条目。我已经清除了DB并进行了一次新的导入,我可以看到所有记录(包括我正在使用的entryID)。我试过提取各种ID号,所有我能确认的都在数据库中,它们都返回undefined。这是我正在使用的代码。

/*
    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的名称)。(在我知道所有其他与DB相关的函数都已完成后手动触发)甚至会触发“onsuccess”,只是结果未定义(好像条目不在数据库中,但我再次检查了它在数据库中)。
根据请求,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创建(需要onupgradened)。

// 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});

        }

    };
yqlxgs2m

yqlxgs2m1#

发现了问题,希望没有人犯同样的简单错误,我没有,但如果有人这样做,并运行在这个问题这里的问题是什么。
我添加到数据库中的键是一个字符串,而我请求的键是一个int。
不要像我一样,检查您的数据类型。

js5cn81o

js5cn81o2#

创建索引/存储时使用适当的keyPath。以下表达式不相同!
第一个
我用数组keyPath创建了一个索引,然后无法从该索引中查询任何内容,即使类型是相等的。
如果您将keyPath定义为数组,则可能应该使用array进行查询:

someIndex.getKey(["value"]))

相关问题