Uncaught TypeError: Cannot read property 'products' of null at addToCart (cart.js:60)
up vote
1
down vote
favorite
hello i am building a cart system using JavaScript localStorage but my script does not work when clear cart using localStorage.clear(); below is my code for adding items to cart and display in cart page.
Code
var cartData = localStorage.getItem("cart");
if (cartData == null) {
cartDataProducts = ;
var cartItems = 0;
$('.mini-cart-number').text(cartItems);
} else {
cartData = JSON.parse(cartData);
cartDataProducts = cartData['products'];
var cartItems = cartDataProducts.length;
$('.mini-cart-number').text(cartItems);
}
function clearCartItemes() {
localStorage.clear();
}
function renderCartItems() {
for (i = 0; i < cartDataProducts.length; i++) {
row = "<tr class='cart_item'> <td>" + i + "</td> <td class='product-thumbnail'> <a href='#'><img src='" + cartDataProducts[i]['img'] + "' alt=''></a> </td> <td class='product-name'> <a href='#'>" + cartDataProducts[i]['name'] + "</a> </td> <td class='product-price'> <span class='amount'>" + cartDataProducts[i]['price'] + "</span> </td> </tr>";
$('#cfxItem').append(row);
}
}
$("body .addcart-link, #addToCartDynamic").click(function() {
name = $(this).data('name');
id = $(this).data('id');
price = $(this).data('price');
img = $(this).data('img');
product = {};
product.id = id;
product.name = name;
product.price = price;
product.img = img;
cartItems = cartItems + 1;
addToCart(product);
});
function addToCart(product) {
if (localStorage && localStorage.getItem('cart')) {
var cart = JSON.parse(localStorage.getItem('cart'));
cart.products.push(product);
localStorage.setItem('cart', JSON.stringify(cart));
$('.mini-cart-number').text(cartItems);
$.notify("Product Added in cart.", "success");
}
}
javascript arrays local-storage
add a comment |
up vote
1
down vote
favorite
hello i am building a cart system using JavaScript localStorage but my script does not work when clear cart using localStorage.clear(); below is my code for adding items to cart and display in cart page.
Code
var cartData = localStorage.getItem("cart");
if (cartData == null) {
cartDataProducts = ;
var cartItems = 0;
$('.mini-cart-number').text(cartItems);
} else {
cartData = JSON.parse(cartData);
cartDataProducts = cartData['products'];
var cartItems = cartDataProducts.length;
$('.mini-cart-number').text(cartItems);
}
function clearCartItemes() {
localStorage.clear();
}
function renderCartItems() {
for (i = 0; i < cartDataProducts.length; i++) {
row = "<tr class='cart_item'> <td>" + i + "</td> <td class='product-thumbnail'> <a href='#'><img src='" + cartDataProducts[i]['img'] + "' alt=''></a> </td> <td class='product-name'> <a href='#'>" + cartDataProducts[i]['name'] + "</a> </td> <td class='product-price'> <span class='amount'>" + cartDataProducts[i]['price'] + "</span> </td> </tr>";
$('#cfxItem').append(row);
}
}
$("body .addcart-link, #addToCartDynamic").click(function() {
name = $(this).data('name');
id = $(this).data('id');
price = $(this).data('price');
img = $(this).data('img');
product = {};
product.id = id;
product.name = name;
product.price = price;
product.img = img;
cartItems = cartItems + 1;
addToCart(product);
});
function addToCart(product) {
if (localStorage && localStorage.getItem('cart')) {
var cart = JSON.parse(localStorage.getItem('cart'));
cart.products.push(product);
localStorage.setItem('cart', JSON.stringify(cart));
$('.mini-cart-number').text(cartItems);
$.notify("Product Added in cart.", "success");
}
}
javascript arrays local-storage
var cart = JSON.parse(localStorage.getItem('cart'));This is null. Did you want to set it to an empty array iflocalStorageis empty?
– CertainPerformance
Nov 12 at 7:49
Have you tried: window.localStorage.clear() ???
– Peter Bode
Nov 12 at 7:50
yes i tried to set it null but then there is some other error
– sam999
Nov 12 at 7:52
@Peter Bode no i did not tried window.localStorage.clear()
– sam999
Nov 12 at 7:52
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
hello i am building a cart system using JavaScript localStorage but my script does not work when clear cart using localStorage.clear(); below is my code for adding items to cart and display in cart page.
Code
var cartData = localStorage.getItem("cart");
if (cartData == null) {
cartDataProducts = ;
var cartItems = 0;
$('.mini-cart-number').text(cartItems);
} else {
cartData = JSON.parse(cartData);
cartDataProducts = cartData['products'];
var cartItems = cartDataProducts.length;
$('.mini-cart-number').text(cartItems);
}
function clearCartItemes() {
localStorage.clear();
}
function renderCartItems() {
for (i = 0; i < cartDataProducts.length; i++) {
row = "<tr class='cart_item'> <td>" + i + "</td> <td class='product-thumbnail'> <a href='#'><img src='" + cartDataProducts[i]['img'] + "' alt=''></a> </td> <td class='product-name'> <a href='#'>" + cartDataProducts[i]['name'] + "</a> </td> <td class='product-price'> <span class='amount'>" + cartDataProducts[i]['price'] + "</span> </td> </tr>";
$('#cfxItem').append(row);
}
}
$("body .addcart-link, #addToCartDynamic").click(function() {
name = $(this).data('name');
id = $(this).data('id');
price = $(this).data('price');
img = $(this).data('img');
product = {};
product.id = id;
product.name = name;
product.price = price;
product.img = img;
cartItems = cartItems + 1;
addToCart(product);
});
function addToCart(product) {
if (localStorage && localStorage.getItem('cart')) {
var cart = JSON.parse(localStorage.getItem('cart'));
cart.products.push(product);
localStorage.setItem('cart', JSON.stringify(cart));
$('.mini-cart-number').text(cartItems);
$.notify("Product Added in cart.", "success");
}
}
javascript arrays local-storage
hello i am building a cart system using JavaScript localStorage but my script does not work when clear cart using localStorage.clear(); below is my code for adding items to cart and display in cart page.
Code
var cartData = localStorage.getItem("cart");
if (cartData == null) {
cartDataProducts = ;
var cartItems = 0;
$('.mini-cart-number').text(cartItems);
} else {
cartData = JSON.parse(cartData);
cartDataProducts = cartData['products'];
var cartItems = cartDataProducts.length;
$('.mini-cart-number').text(cartItems);
}
function clearCartItemes() {
localStorage.clear();
}
function renderCartItems() {
for (i = 0; i < cartDataProducts.length; i++) {
row = "<tr class='cart_item'> <td>" + i + "</td> <td class='product-thumbnail'> <a href='#'><img src='" + cartDataProducts[i]['img'] + "' alt=''></a> </td> <td class='product-name'> <a href='#'>" + cartDataProducts[i]['name'] + "</a> </td> <td class='product-price'> <span class='amount'>" + cartDataProducts[i]['price'] + "</span> </td> </tr>";
$('#cfxItem').append(row);
}
}
$("body .addcart-link, #addToCartDynamic").click(function() {
name = $(this).data('name');
id = $(this).data('id');
price = $(this).data('price');
img = $(this).data('img');
product = {};
product.id = id;
product.name = name;
product.price = price;
product.img = img;
cartItems = cartItems + 1;
addToCart(product);
});
function addToCart(product) {
if (localStorage && localStorage.getItem('cart')) {
var cart = JSON.parse(localStorage.getItem('cart'));
cart.products.push(product);
localStorage.setItem('cart', JSON.stringify(cart));
$('.mini-cart-number').text(cartItems);
$.notify("Product Added in cart.", "success");
}
}
javascript arrays local-storage
javascript arrays local-storage
asked Nov 12 at 7:46
sam999
487
487
var cart = JSON.parse(localStorage.getItem('cart'));This is null. Did you want to set it to an empty array iflocalStorageis empty?
– CertainPerformance
Nov 12 at 7:49
Have you tried: window.localStorage.clear() ???
– Peter Bode
Nov 12 at 7:50
yes i tried to set it null but then there is some other error
– sam999
Nov 12 at 7:52
@Peter Bode no i did not tried window.localStorage.clear()
– sam999
Nov 12 at 7:52
add a comment |
var cart = JSON.parse(localStorage.getItem('cart'));This is null. Did you want to set it to an empty array iflocalStorageis empty?
– CertainPerformance
Nov 12 at 7:49
Have you tried: window.localStorage.clear() ???
– Peter Bode
Nov 12 at 7:50
yes i tried to set it null but then there is some other error
– sam999
Nov 12 at 7:52
@Peter Bode no i did not tried window.localStorage.clear()
– sam999
Nov 12 at 7:52
var cart = JSON.parse(localStorage.getItem('cart')); This is null. Did you want to set it to an empty array if localStorage is empty?– CertainPerformance
Nov 12 at 7:49
var cart = JSON.parse(localStorage.getItem('cart')); This is null. Did you want to set it to an empty array if localStorage is empty?– CertainPerformance
Nov 12 at 7:49
Have you tried: window.localStorage.clear() ???
– Peter Bode
Nov 12 at 7:50
Have you tried: window.localStorage.clear() ???
– Peter Bode
Nov 12 at 7:50
yes i tried to set it null but then there is some other error
– sam999
Nov 12 at 7:52
yes i tried to set it null but then there is some other error
– sam999
Nov 12 at 7:52
@Peter Bode no i did not tried window.localStorage.clear()
– sam999
Nov 12 at 7:52
@Peter Bode no i did not tried window.localStorage.clear()
– sam999
Nov 12 at 7:52
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257783%2funcaught-typeerror-cannot-read-property-products-of-null-at-addtocart-cart-j%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257783%2funcaught-typeerror-cannot-read-property-products-of-null-at-addtocart-cart-j%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
var cart = JSON.parse(localStorage.getItem('cart'));This is null. Did you want to set it to an empty array iflocalStorageis empty?– CertainPerformance
Nov 12 at 7:49
Have you tried: window.localStorage.clear() ???
– Peter Bode
Nov 12 at 7:50
yes i tried to set it null but then there is some other error
– sam999
Nov 12 at 7:52
@Peter Bode no i did not tried window.localStorage.clear()
– sam999
Nov 12 at 7:52