问题
My object structure is like this:
var test={
"id":"A",
"ChanName":"Discovery",
"LCN":10
};
This is the snippet that creates the object store:
var objectStore = db.createObjectStore('Dat', { keyPath:'test.id',autoIncrement: false});
var trans = db.transaction(["Dat"], webkitIDBTransaction.READ_WRITE);
var store = trans.objectStore("Dat");
var request=store.put(test);
When i try to add the test object i get this exception DATA_ERR: DOM IDBDatabase Exception 5. Please can you advice on what is wrong here? I am using chrome 18 to test this.The creation of object store is fine,but not able to add data
回答1:
An IndexedDB Exception 5 is thrown when the "Data provided to an operation does not meet requirements." This is typically because you've added an index, for example, while providing an object missing that attribute.
Here, it appears your test
object doesn't match what you've specified as a keypath.
If you want id
as a key your createObjectStore
would have to be like this:
var objectStore = db.createObjectStore('Dat', { keyPath:'id',autoIncrement: false});
As a further example, alternatively, with your keyPath as is your test
object would need to look like this to not throw this error:
var test={
test: { "id":"A" },
ChanName:"Discovery",
LCN:10
};
来源:https://stackoverflow.com/questions/10509546/dom-idbdatabase-exception-5-when-adding-data-in-indexeddb