问题
I have no idea what's wrong with this code:
onupgradeneeded = function(){
z = e.currentTarget.result.createObjectStore(
'record',
{keyPath:'id',autoIncrement:true}
);
z.createIndex('book', 'book', {unique:false});
z.createIndex('user', 'user', {unique:false});
}
When I try to put data into the object store using the following code:
db.transaction('record',IDBTransaction.READ_WRITE)
.objectStore('record')
.add({book:...,user:...})
I receive the following error message:
Data provided to an operation does not meet requirements
回答1:
createIndex
needs to be called from within a setVersion
transaction (spec previous to Dec. 2011, supported by current versions of Chrome and IE) and from a onupgradeneeded
callback in the latest spec (currently supported only by FF, but IE10 and later versions of Chrome will upgrade to this).
The spec description of createIndex
lays out why you're seeing the error you're seeing:
createIndex: This method creates and returns a new index with the given name and parameters in the connected database. If this function is called from outside a VERSION_CHANGE transaction callback, the implementation must throw an DOMException of type InvalidStateError exception.
For more info on the difference between setVersion
and onupgradeneeded
, check out this post from IE folks, which explains the changes in code.
来源:https://stackoverflow.com/questions/9900705/error-data-provided-to-an-operation-does-not-meet-requirements-when-trying-to