How do I specify key when adding records to objectStore w/ in IndexedDB?

自古美人都是妖i 提交于 2019-12-10 09:45:11

问题


Trying to learn the concepts and API of IndexedDB and I'm struggling trying to figure out how to specify keys for an objectStore using the IDBObjectStore.add method. According to the spec, the first parameter is the value and the second optional parameter is the key. I can add a record when I supply an object that has Bar as a property of the value (which is an object), but when I try to pass the key in through an object via the second parameter, the add attempt fails and the details that I get are:

Code: 5. Message: DataError: DOM IDBDatabase Exception 5. Name: DataError. Stack: Error: The data provided does not meet requirements. at IDBOpenDBRequest.dbOpenRequest.onsucces

Code sample is below:

var dbOpenRequest = window.indexedDB.open("sandbox")

    dbOpenRequest.onupgradeneeded = function (event) {
        var db = dbOpenRequest.result;

        var fooObjStore = db.createObjectStore("Foo", {
            keyPath: "Bar",
            autoIncrement: false
        });
    }

    dbOpenRequest.onsuccess = function (event) {
        var db = dbOpenRequest.result;
        var transaction = db.transaction(["Foo"], "readwrite");
        transaction.oncomplete = function () {
            console.log("Transaction complete");
        }            

        transaction.onerror = function (event) {
            console.error("Transaction error! " + event.target.webkitErrorMessage);
        }

        var fooObjStore = transaction.objectStore("Foo");
        try {
            //Works
            var fooRequest = pipelineObjStore.add({ data: "myData", Bar: "1" });

            //Fails
            fooRequest = pipelineObjStore.add({ data: "myData" }, "2" );

            //Fails
            fooRequest = pipelineObjStore.add({ data: "myData" }, { Bar: "3" });
        }
        catch (e) {
            console.error("Code: " + e.code
                + ". \nMessage: " + e.message
                + ". \nName: " + e.name
                + ". \nStack: " + e.stack);
        }

        fooRequest.onsuccess = function (event) {
            console.log("Pipeline request successful");
        }

        fooRequest.onerror = function (event) {
            console.error("Pipeline request error. " + event.target.webkitErrorMessage);
        }
    }

    dbOpenRequest.onerror = function (event) {
        console("Error ");
    }

What I ultimately want to do is create an objectStore that has strings for it's values and supply the key separately. Is it possible to do this or do I have to supply an object that contains my string data as part of a property and another property that has the key?


回答1:


You are using in-line key. But your case requires object store with out-of-line key.

var fooObjStore = db.createObjectStore("Foo", {
  autoIncrement: false 
});

Then you add record with your external key, "1" here.

var fooRequest = fooObjStore.add({data: "myData"}, "1");


来源:https://stackoverflow.com/questions/14348497/how-do-i-specify-key-when-adding-records-to-objectstore-w-in-indexeddb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!