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 ");
-
- localStorage.setItem(this.storageKey, JSON.stringify(this.items));
- }
- });
- }