0

I am developing an eCommerce website asp.net mvc with angularjs. I am getting some issue with $scope in one of my page.I define an $scope.items array in my controller and trying to fetch values in this array from cookies. here is the code

app.controller('checkoutController', ['$scope', 'productService', '$cookies', '$cookieStore', '$location', 'sharedService', '$rootScope', 'customerService', function ($scope, productService, $cookies, $cookieStore, $location, sharedService, $rootScope, customerService) {
    $scope.items = [];

    $scope.customer = {};
    $scope.customer.FirstName = "";
    $scope.customer.LastName = "";
    $scope.customer.Email = "";
    $scope.customer.Password = "";
    $scope.customer.CPassword = "";
    $scope.customer.Address1 = "";
    $scope.customer.Address2 = "";
    $scope.customer.ZipCode = "";
    $scope.customer.Country = "";
    $scope.customer.State = "";
    $scope.customer.City = "";
    $scope.customer.MobileNo = "";

    $scope.logDetails = {};
    $scope.logDetails.LogEmail = "";
    $scope.logDetails.LogPassword = "";
    $scope.customer.Roleid = 1; 
   $scope.items = $cookies.StyleStoreCart;
}

it gives me error that $scope.items is not defined. but its already defined

Experts please tell me where i m going wrong?

here is the url of live website. Please try to checkout and see error in console on checkout page.

http://stylesstore.com/Home/Products

here is the way I am inserting data to cookies in my application in my product page

var item = new cartItem(Picture1, ProductName, UnitPrice);
        $scope.items.push(item); 
        $cookies.StyleStoreCart = $scope.items 
        $scope.Itemcount = $scope.Itemcount + 1

and in my checkout page I am getting the cookies values like this

 $scope.items = $cookies.StyleStoreCart;

1 Answer 1

1

This is because items is assigned to undefined before you access it.

In your first line of code, you initialize items as an array, but $cookieStore.get('StyleStoreCart') is undefined, then $scope.items became undefined after this assignment.

Update

Home page stores StyleStoreCart in cookies successfully, but if you take a look at this cookie item using developer tool (in Chrome, it's chrome://settings/cookies), you'll see this cookie's path is /home. Then you navigate to http://stylesstore.com/Cart/CheckOut, whose path is /cart, that's maybe we can't get StyleStoreCart from cookies.

A proper solution is, store cookies as below

$cookies.put('StyleStoreCart', value, {path: "/"}); 
Sign up to request clarification or add additional context in comments.

4 Comments

but m getting this from cookies and my cookies is not null.. :(
but from the website, $cookieStore.get('StyleStoreCart') turns out to be undefined
i am also trying to find the reason .. from the product page it is assigning data to the cookies but in checkout page its show undefined.. I donot know why. I update my question please see how I am assigning and getting data from cookies
thanks I will try it and update u what was the result :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.