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
Cannot read property 'storageKey' of undefined
Aug 7 2020 2:28 PM
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));
}
});
}
Reply
Answers (
3
)
Send alert if item has already been selected
Don't Add Product if Product is already in localStorage