I created an object store with autoIncrement: db.createObjectStore("items", {autoIncrement:true});
Now I want to be able to update an item given its key and a new value, so I wrote this function:
let updateItem = (key, newData) => {
let objectStore = db.transaction(["items"], "readwrite").objectStore("items");
let request = objectStore.get(key);
request.onsuccess = (e) => {
let data = e.target.result;
Object.assign(data, newData);
let requestUpdate = objectStore.put(data);
};
}
However, instead of updating the value, it creates a new item with the new data. I think it makes sense since e.target.result
does not contain any information about its key. So how do I update an element in such an object store?