Javascript TypeError: 'undefined' is not an object (evaluating...

rito1
rito1 used Ask the Experts™
on
Hi All,

Can anyone help with the school boy error please... I have declared a new object and then return stuff for the property from the function _Mgt_TrackCurrentSessionLocalStorageManagement().

Now, I know there isn't anything to return currently so I believe the object _Mgt_TrackSession should be undefined.

Can anyone see why it would trip up which creating the first property of my object?

_Mgt_TrackSession = new Object();
_Mgt_TrackSession = _Mgt_TrackCurrentSessionLocalStorageManagement("get");

if (typeof(_Mgt_TrackSession) === 'undefined') 
{
     _Mgt_TrackSession.A = intAppID;
     _Mgt_TrackSession.U = intUserID;
     _Mgt_TrackSession.P = [];
}

Open in new window



Error:


TypeError: 'undefined' is not an object (evaluating '_Mgt_TrackSession.A = intAppID')
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I think your _Mgt_TrackSession is undefined.  You instantiate it as an object, but then you assign the result of some function to it, making it undefined - not an object.  Then you try to set properties on undefined, which is not allowed.

perhaps try it like this:

_Mgt_TrackSession = new Object();
_Mgt_TrackSession = _Mgt_TrackCurrentSessionLocalStorageManagement("get");

if (typeof(_Mgt_TrackSession) === 'undefined')
{
     _Mgt_TrackSession = new Object();
     _Mgt_TrackSession.A = intAppID;
     _Mgt_TrackSession.U = intUserID;
     _Mgt_TrackSession.P = [];
}

Author

Commented:
Thanks onlyaymi, I will give this a try as soon as I get in the office in the morning GMT.

I didn't think I would have to declare it again but it does make sense.. On some occasions my function will return an object for _Mgt_TrackSession. So if there is nothing to return I will need to instantiate it again.. Is that right?
I believe so, b/c the _Mgt_TrackSession is set to whatever the function returns, so if the function returns nothing (undefined), _Mgt_TrackSession's value is then undefined, not the object you originally instantiated it as.

Author

Commented:
Perfect, thank you for your time.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial