I have a shopping cart and I only want the products to be set if they don’t already exist in localStorage. If the product is already in localStorage, then no new product should be added. Later I will update the quantity if the product already exists in localStorage but I haven’t reached that step yet. When I try to set the item when the id isn’t in localStorage, I get the TypeError: Cannot read property ‘storageKey’ of undefined
storageKey = 'MyDataStorageKey';
public onSubmit(id, quantity, product_name){
var data = {
id,
quantity,
product_name,
};
var retrieverObject = localStorage.getItem('items');
var retrieveObject = JSON.parse(retrieverObject);
this.items.push(data);
retrieveObject.forEach(function(el){
alert("This is the storage id " + el.id);
if (el.id == id){
alert(el.id + " same product");
} else {
alert("this id is not in storage ");
//error returns for the following line of code
localStorage.setItem(this.storageKey, JSON.stringify(this.items));
}
});
}
I have a shopping cart and I only want the products to be set if they don’t already exist in localStorage. If the product is already in localStorage, then no new product should be added. Then I need to update the quantity if the item is already set. The code that I have so far sets the product even if the product already exists and it doesn’t update the quantity.
public onSubmit(id, thumbnail, quantity, product_name, product_price){
var data = {
id,
thumbnail,
quantity,
product_name,
product_price
};
var retrieverObject = localStorage.getItem('items');
var retrieveObject = JSON.parse(retrieverObject);
retrieveObject.forEach(el => {
if (el.id == id){
this.quantity += quantity;
} else {
this.items.push(data);
localStorage.setItem(this.storageKey, JSON.stringify(this.items));
}
});
}
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);
}
My localhost isn’t working any more. I shut down the McAfee firewall, re-started my computer and reverted back to the code that had worked before but my localhost still shows a blank page.