Question

I want to know how to update IndexedDB records like (UPDATE TABLE SET column_name= MyNewValue WHERE ID=MyKey). I want to update single attribute in a object using the key..

While trying to Update using Cursor, I'm getting this error,

Uncaught DataError: Failed to execute 'update' on 'IDBCursor': The effective object store of this cursor uses in-line keys and evaluating the key path of the value parameter results in a different value than the cursor's effective key.

Was it helpful?

Solution

Thanks for including your error. I can't say with certainty without seeing your object store creation code but I'm very familiar with this type of issue.

What it means is that you're not using auto-incrementing keys, and providing a key yourself. That's very normal. In that case, you use the following IDBCursor.update() signature.

cursor.update(your_updated_entry_object);

IDB knows which entry to update because your key is "in-line" on the your_updated_entry_object (meaning your_updated_entry_object has an attribute that is your key).

It sounds like you may have seen the IDBStore.put() method and been confused. That takes on two seperate signatures for so-called "inline keys" (what you have) and "out-of-line" keys (what you get if you let IDB autoincrement the keys for you).

With in-line keys, it's the same signature as IDBCursor.update():

store.put(your_updated_entry_object);

However with out-of-line keys, it takes on an extra key param that tells IDB which object to update (since you're not on a cursor and otherwise has no context for your request):

store.put(your-updated_entry_object, your_autoincremented_key);

OTHER TIPS

Yeah, update method on the cursor is misleading, because it suggests inline primary key can be change as well. There is a lengthy discussion whether to allow changing primary key according to the method.

Actually if primary key were allowed to be changed, it is even more confusing, so it is not allowed. The only way is delete and create a new one. It is also same concept for other key-value store as well.

If the effective object store of this cursor uses in-line keys and evaluating the key path of the value parameter results in a different value than the cursor's effective key, the implementation must throw a DOMException of type DataError.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top