16

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.

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • 1
    I was facing the same problem.I tried IDBRequest put (any value, optional any key); with passing autoincrement id in the optional parameter and it worked – Rajat Talwar Mar 09 '14 at 08:41

2 Answers2

25

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.

  • Thank you! If I had created the objectStore with `{ autoIncrement: true }` would this have worked? – Don Rhummy Jun 16 '13 at 07:43
  • 3
    Without the key-path, yes, each object store gets its own key-generator. Also see this article: http://www.kristofdegrave.be/2012/02/indexed-db-to-provide-key-or-not-to.html –  Jun 16 '13 at 08:09
  • Thank you for the full list link, that showed me how to implement out-of-line keys. – Jacques Olivier Apr 26 '19 at 14:18
  • @JacquesOlivier lol "That [link] showed me how to implement out-of-line keys." The link is dead. What was in it? :) – user1477388 Oct 25 '19 at 19:33
  • 1
    @user1477388 what specifically do you need to know? how to use out-of-line keys or the fix to the question? – Jacques Olivier Nov 07 '19 at 12:59
  • 1
    @JacquesOlivier Thanks for following up. I was able to resolve my issue by creating my object store using `dataBase.createObjectStore("FileAccessHistory", { keyPath: 'key', autoIncrement: true });` and then inserting records with `transaction.objectStore("FileAccessHistory").put({ accessedTimestampUtc: new Date().toISOString(), fileId: file.key });`. – user1477388 Nov 07 '19 at 20:55
  • 1
    @user1477388 Cool, I'm glad that you've managed to resolve your issue. Just out of interest sake, I wanted to manage my own keys, so I created my object store like this - `upgradeDB.createObjectStore('StaticData');` - and I inserted my objects like this - `tx.objectStore('StaticData').put(item.encryptedData, item.itemId);` - this way the autoIncrement and keyPath is 'turned-off' and i was able to manage it myself. it might not be the best approach but for this specific instance its what I needed. – Jacques Olivier Nov 08 '19 at 09:41
4

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.

Andrew Shustariov
  • 2,530
  • 1
  • 17
  • 17