Indexeddb - Update record by index key -
i have object store keypath "id" (autoincrement), , unique index on store keypath "myid" generated server. when need update record, won't have "id" available, how can update record using "myid" considering index unique?
i tried using mystore.put(record), gives error since record "myid" exists.
since you've got unique key index on myid
property can retrieve record based on value. you'll need use only
idbkeyrange
, cursor
query, so:
var transaction = db.transaction('mystore'); var store = transaction.objectstore('mystore'); var index = store.index('myid_index'); var keyrange = idbkeyrange.only(obj.myid); var request = index.opencursor(keyrange); request.onsucess = function (e) { var cursor = e.target.result; if (cursor) { var item = cursor.value; obj.id = item.id; //save object } };
also, if want update using same transaction you'll need make readwrite
not readonly
did.
Comments
Post a Comment