错误“提供给操作的数据不符合要求".尝试将数据添加到indexedDB时 [英] Error "Data provided to an operation does not meet requirements" when trying to add data to indexedDB
问题描述
我不知道这段代码怎么了:
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:...})
我收到以下错误消息:
Data provided to an operation does not meet requirements
推荐答案
createIndex
需要从setVersion
事务(2011年12月之前的规范,当前版本的Chrome和IE支持)中调用,最新规范中的onupgradeneeded
回调(目前仅受FF支持,但IE10和更高版本的Chrome将会升级到该回调).
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).
规范对createIndex
的描述列出了为什么看到错误的原因:
The spec description of createIndex
lays out why you're seeing the error you're seeing:
createIndex:此方法使用给定的索引创建并返回一个新索引 连接数据库中的名称和参数.如果这个功能是 从VERSION_CHANGE事务回调外部调用, 实现必须抛出InvalidStateError类型的DOMException
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.
有关setVersion
和onupgradeneeded
之间差异的更多信息,请查看
For more info on the difference between setVersion
and onupgradeneeded
, check out this post from IE folks, which explains the changes in code.
这篇关于错误“提供给操作的数据不符合要求".尝试将数据添加到indexedDB时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!