TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Maureen Moore
NA
206
0
Don't Add Product if Product is already in localStorage
Aug 8 2020 8:51 AM
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));
}
});
}
Reply
Answers (
3
)
Cannot read property 'storageKey' of undefined
How can we Post Three components data(form page) into the database?