The following code only works when I force it to reload at the end. How can I get the code to run without forcing it to reload?
public onSubmit(id, thumbnail, quantity, product_name, product_price){
this.product_price = parseFloat(product_price);
var data = {
id,
thumbnail,
quantity,
product_name,
product_price
};
this.isSubmitted = true;
// Get the saved stringified object from cache
const retrieverObject: string = localStorage.getItem(this.storageKey) || '';
// Parse it to an array, or set to a blank array, if there is no data
const retrieveObject: Array<any> = retrieverObject ? JSON.parse(retrieverObject) : [];
// Find the item from the array
let presentItem = retrieveObject.find(item => item.id === id);
// Update the data, if the item is found
if (presentItem) {
//this.quantity += quantity;
// If the stored objects quantity needs to be updated
presentItem.quantity += quantity;
} else {
// Else if not found in the saved array, push the new object
retrieveObject.push(data);
}
// Set the new object to the same key
localStorage.setItem(this.storageKey, JSON.stringify(retrieveObject));
location.reload(true);
}