How to update IndexedDB

Mac Cruz
Mac Cruz used Ask the Experts™
on
Hi experts, I want to update my IndexedDb that the value of msales be updated and be added to 30.00 to become 250.00 in result and the value of created will remain.

My IndexdDB
key = 1975 value = {msales: "220.00", created: "11-18-2019"}

Js
function gsaleslocal(){
	var sales = "30.00"

	var transaction = db.transaction(["gsales"],"readwrite");
	var store = transaction.objectStore("gsales");

	var mytrx = {
	    msales: sales,
	};

	var request = store.put(mytrx,1975);
	
	request.onerror = function(e) {
			console.log("Error", e.target.error.name);
	}
	request.onsuccess = function(e) {
			console.log("Successfully added");
	}
};

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
check example here : https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/put

function gsaleslocal(){
    var sales = "30.00"

    var transaction = db.transaction(["gsales"],"readwrite");
    var store = transaction.objectStore("gsales");

    var request = store.get(1975);
    
    request.onerror = function(e) {
        console.log("Error", e.target.error.name);
    }

    request.onsuccess = function(e) {
        var data = request.result;
        data.msales = sales;
        store.put(mytrx);
        console.log("Successfully added");
    }
};

Open in new window

Author

Commented:
Thank you leakim971, your solution lead me to solve the problem..
leakim971Multitechnician
Top Expert 2014

Commented:
you welcome, have a nice day

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial