Why is this IndexedDB put command failing? Error: DataError: DOM IDBDatabase Exception 0

假如想象 提交于 2019-12-06 19:27:30

问题


I have successfully added the following to the objectStore when I created it:

{ 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:

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 spec states that the second parameter should be allowed:

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.


回答1:


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.




回答2:


I met interesting behavior of this function in 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.

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.

Here is an example.



来源:https://stackoverflow.com/questions/17131015/why-is-this-indexeddb-put-command-failing-error-dataerror-dom-idbdatabase-exc

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