locked
Does sessionState not work for array properties?

    Question

  • Scenario:

    Whenever I favorite a Link, I wish to save the link's session data. Bellow is my highlevel code:

    favoriteClick : function(mouseEvent)

    {

    --- create dynamic HTML ---

    -- add HTML with following syntax : <li><a id="foo_id" href="foo_href">foo content</a></li>-----

    WinJS.Application.sessionState.fooId[counter] = foo_id; 

    WinJS.Application.sessionState.fooHref[counter] = foo_href; 

    }

    I get the following error
    "0x800a138f - JavaScript runtime error: Unable to set property '0' of undefined or null reference"

    at the line - "WinJS.Application.sessionState.fooId[counter] = foo_id; "


    I DO NOT get an error if I use a unique variable. So
    "WinJS.Application.sessionState.fooIdUnique = foo_id; " does not return an error.

    Hoping someone can shed some light on the issue at hand.

    Thanks!

    Friday, January 25, 2013 10:21 PM

Answers

  • I downloaded your project and see that clicking the "Favorite It" link calls the function favoriteClick in home.js.

    In that function you are assigning the following lines that are currently commented out like this:

                favLinkIdArray[favLinkCount] = favLinkId;
                favLinkLinkArray[favLinkCount] = linkModified;
                favLinkHrefArray[favLinkCount] = link;
                //WinJS.Application.sessionState.favLinkIdArray[favLinkCount] = favLinkId;
                //WinJS.Application.sessionState.favLinkTextArray[favLinkCount] = linkModified;
                //WinJS.Application.sessionState.favLinkHrefArray[favLinkCount] = link;

    If I uncomment the 3 lines above I get the error. However, this error is expected since you have instantiated the favLinkIdArray as a "global" variable of the type array but you have NOT created the array for the favLinkIdArray that is a member of the sessionState object (i.e WinJS.Application.sessionState.favLinkIdArray is an un-defined array). The arrays are declared like this in home.js:

    var favLinkIdArray = new Array();
    var favLinkLinkArray = new Array();
    var favLinkHrefArray = new Array();

    You should be able to resolve the issue by doing this:

                //favLinkIdArray[favLinkCount] = favLinkId;
                //favLinkLinkArray[favLinkCount] = linkModified;
                //favLinkHrefArray[favLinkCount] = link;

                if (WinJS.Application.sessionState.favLinkIdArray == null) {
                    WinJS.Application.sessionState.favLinkIdArray = new Array();
                }
                if (WinJS.Application.sessionState.favLinkTextArray == null) {
                    WinJS.Application.sessionState.favLinkTextArray = new Array();
                }
                if (WinJS.Application.sessionState.favLinkHrefArray == null) {
                    WinJS.Application.sessionState.favLinkHrefArray = new Array();
                }
                WinJS.Application.sessionState.favLinkIdArray[favLinkCount] = favLinkId;
                WinJS.Application.sessionState.favLinkTextArray[favLinkCount] = linkModified;
                WinJS.Application.sessionState.favLinkHrefArray[favLinkCount] = link;

    • Marked as answer by Jay_Hawk Tuesday, January 29, 2013 5:19 PM
    Tuesday, January 29, 2013 1:26 AM
    Moderator

All replies

  • You need to initialize the array before assigning it. Does the below approach work?

            // declare the array first before assigning.

            WinJS.Application.sessionState.fooId = new Array();
            WinJS.Application.sessionState.fooHref = new Array();

            WinJS.Application.sessionState.fooId[counter] = foo_id;
            WinJS.Application.sessionState.fooHref[counter] = foo_href;

    Thanks,

    Prashant.

    Saturday, January 26, 2013 1:27 AM
    Moderator
  • Unfortunately it does not. I still continue to get the same error. 

    Thanks!

    Saturday, January 26, 2013 7:35 PM
  • In that case, please upload your demo project to a Skydrive location.

    Thanks,

    Prashant.

    Monday, January 28, 2013 5:53 PM
    Moderator
  • I have uploaded the project to Skydrive - http://sdrv.ms/11345Xx.

    Since the project is still in the early stages, most of the features are still half baked. I have included steps in "instructions.txt" on how one can repro the issue I am hitting.

    Thank You!

    Monday, January 28, 2013 6:42 PM
  • The uploaded project is incomplete, I cannot compile it. It does not include any JS files or necessary dependencies. The best approach to upload your project is to zip the "entire" project with the dependencies, as well as any folders and then share it.
    Monday, January 28, 2013 8:20 PM
    Moderator
  • My bad. I have uploaded the project zip file. Please could you try again.

    Thanks!

    Monday, January 28, 2013 10:17 PM
  • I downloaded your project and see that clicking the "Favorite It" link calls the function favoriteClick in home.js.

    In that function you are assigning the following lines that are currently commented out like this:

                favLinkIdArray[favLinkCount] = favLinkId;
                favLinkLinkArray[favLinkCount] = linkModified;
                favLinkHrefArray[favLinkCount] = link;
                //WinJS.Application.sessionState.favLinkIdArray[favLinkCount] = favLinkId;
                //WinJS.Application.sessionState.favLinkTextArray[favLinkCount] = linkModified;
                //WinJS.Application.sessionState.favLinkHrefArray[favLinkCount] = link;

    If I uncomment the 3 lines above I get the error. However, this error is expected since you have instantiated the favLinkIdArray as a "global" variable of the type array but you have NOT created the array for the favLinkIdArray that is a member of the sessionState object (i.e WinJS.Application.sessionState.favLinkIdArray is an un-defined array). The arrays are declared like this in home.js:

    var favLinkIdArray = new Array();
    var favLinkLinkArray = new Array();
    var favLinkHrefArray = new Array();

    You should be able to resolve the issue by doing this:

                //favLinkIdArray[favLinkCount] = favLinkId;
                //favLinkLinkArray[favLinkCount] = linkModified;
                //favLinkHrefArray[favLinkCount] = link;

                if (WinJS.Application.sessionState.favLinkIdArray == null) {
                    WinJS.Application.sessionState.favLinkIdArray = new Array();
                }
                if (WinJS.Application.sessionState.favLinkTextArray == null) {
                    WinJS.Application.sessionState.favLinkTextArray = new Array();
                }
                if (WinJS.Application.sessionState.favLinkHrefArray == null) {
                    WinJS.Application.sessionState.favLinkHrefArray = new Array();
                }
                WinJS.Application.sessionState.favLinkIdArray[favLinkCount] = favLinkId;
                WinJS.Application.sessionState.favLinkTextArray[favLinkCount] = linkModified;
                WinJS.Application.sessionState.favLinkHrefArray[favLinkCount] = link;

    • Marked as answer by Jay_Hawk Tuesday, January 29, 2013 5:19 PM
    Tuesday, January 29, 2013 1:26 AM
    Moderator
  • Got it working. Thanks a lot Prashant!

    Tuesday, January 29, 2013 5:19 PM