Html 为什么这个 IndexedDB put 命令失败?错误:数据错误:DOM IDBDatabase 异常 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17131015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is this IndexedDB put command failing? Error: DataError: DOM IDBDatabase Exception 0
提问by Don Rhummy
I have successfully added the following to the objectStore when I created it:
我在创建 objectStore 时已成功添加以下内容:
{ name: "John Doe", age: 21 }
I used the options:
我使用了以下选项:
{ keyPath: "id", autoIncrement: true }
I am able to find that record and it shows the id = 1
. However, when I run this command below, it throws an error:
我能够找到该记录,它显示了id = 1
. 但是,当我在下面运行此命令时,它会引发错误:
var store = db.transaction( [ "employees" ], "readwrite" ).objectStore( "employees" );
var request = store.put( { name: "John Doe", age: 32 }, 1 );
This throws:
这抛出:
DataError: DOM IDBDatabase Exception 0
Does anyone know what's wrong? Am I specifying the key incorrectly?
有谁知道出了什么问题?我是否错误地指定了密钥?
Update
更新
The IndexedDB specstates that the second parameter should be allowed:
在IndexedDB的规范规定,第二个参数应该被允许:
interface IDBObjectStore {
...
IDBRequest put (any value, optional any key);
...
};
However, it doesn't work, but this does work:
但是,它不起作用,但这确实有效:
store.put( { name: "John Doe", age: 32, id: 1 } );
That is a bug to require that. Unless I'm still doing something incorrectly.
这是一个错误要求。除非我仍然在做一些不正确的事情。
回答by
The error means (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.
对象存储使用内联键并提供了键参数。
You are specifying a keypath
which instruct the store to use in-line keys, but as you specified an out-of-line key as second parameter to put
it will fail.
您正在指定keypath
指示商店使用内联键的 a,但是当您将外联键指定为put
它的第二个参数时,它将失败。
回答by Andrew Shustariov
I met interesting behavior of this function in Internet Explorer 10.
我在 Internet Explorer 10 中遇到了这个函数的有趣行为。
I had simple storage with key configuration like yours:
我有像你这样的关键配置的简单存储:
{ keyPath: "id", autoIncrement: true }
When trying to put object into it and passing second argument as variable, which has value undefined, DataError exception is raised. Firefox and Google Chrome do not have such strange behavior.
当尝试将对象放入其中并将第二个参数作为变量传递时,其值为undefined,会引发 DataError 异常。Firefox 和 Google Chrome 没有这种奇怪的行为。
It seems, IE10 checks arguments length in its implementation instead of checking if second argument is defined. I had a lot of problems because of it, so, I hope, my answer will help other peaple, who will face this exception in IE10.
看来,IE10 在其实现中检查参数长度,而不是检查是否定义了第二个参数。因为它我遇到了很多问题,所以,我希望,我的回答能帮助其他人,他们将在 IE10 中面临这个异常。
Here is an example.
这是一个例子。