NVO NVO - 2 months ago 7
AngularJS Question

array.push is not a function

I want to add a new value to a array in Javascript with the following code:

$rootScope.shoppingCart = new Array();

$rootScope.addToShoppingCart = function(item){
var quantity = 1;
var product = [];

for(var i=0; i< $rootScope.shoppingCart.length; i++){
if($rootScope.shoppingCart[i].productNumber.indexOf(item.product.productNumber) > -1){
quantity = $rootScope.shoppingCart[i].quantity+1;
$rootScope.shoppingCart[i].quantity = quantity;
break;
}
}
if(quantity == 1){
product = {
name: item.product.brandName + " "+ item.product.productNumber,
price: item.price,
image: item.product.image1,
quantity: quantity,
productNumber: item.product.productNumber,
productId: item.product._id
};
$rootScope.shoppingCart.push(product);
}
}


But when I want to add a new product to the array via the '
addToShoppingCart
' function I get an
$rootScope.shoppingCart.push is not a function
error.
I my eyes I do not anything wrong because I want to push an array into an array, but it keeps coming with this error.

Is there anybody who sees what I'm doing wrong here?

Answer

You may simply add the following code to 'loadcart' function

Array.prototype.push.apply($rootscope.shoppingCart, response.data)
Comments