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?
add() adds a new record to the store.
So to summarize, indexName is just what you call the index you created that you want to search, and keyPath is the actual name of the stored data on which you want to search.
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.
To use a cursor , it must first be created with the openCursor() method of the objectStore object, once the request is made, the success event is handled where the result of the request is the cursor , within this result we can access the stored data or only its key .
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");
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