Error “Data provided to an operation does not meet requirements” when trying to add data to indexedDB

浪子不回头ぞ 提交于 2019-12-11 03:48:34

问题


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

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