我在创建它时已成功将以下内容添加到objectStore:
{ name: "John Doe",age: 21 }
我使用了以下选项:
{ keyPath: "id",autoIncrement: true }
我能够找到该记录并显示id = 1.但是,当我在下面运行此命令时,它会抛出一个错误:
var store = db.transaction( [ "employees" ],"readwrite" ).objectStore( "employees" ); var request = store.put( { name: "John Doe",age: 32 },1 );
抛出:
DataError: DOM IDBDatabase Exception 0
有谁知道什么是错的?我是否错误地指定了密钥?
更新
IndexedDB spec声明应该允许第二个参数:
interface IDBObjectStore { ... IDBRequest put (any value,optional any key); ... };
但是,它不起作用,但这确实有效:
store.put( { name: "John Doe",age: 32,id: 1 } );
这是一个要求它的错误.除非我还在做错事.
解决方法
错误意味着(
see here for full list):
Data provided to an operation does not meet requirements.
The object store uses in-line keys and the key parameter was provided.
您正在指定一个指示商店使用in-line keys的密钥路径,但是当您指定一个外联键作为第二个参数时,它将失败.