Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StructKeyExists problems 1

Status
Not open for further replies.

jimmyshoes

Programmer
Jun 1, 2008
132
GB

I am checking for the existence of somevar
The following code works fine
Code:
<cfif not isDefined('session.user.somevar')>

The problem is I want to use structKeyExists(..) but the user struct is not always defined, so if I try the followng then I sometimes get the error Element USER is undefined in SESSION.
Code:
 <cfif not structKeyExists(session.user,'somevar')>

This code is repeated a lot and I read the structKeyExists() is more efficient. How can I use it in this scenario?
 
the user struct is not always defined, so if I try the followng then I sometimes get the error Element USER is undefined in SESSION.

Then you need to verify both keys, starting with the top level element: session.user. Not tested, but something like this

Code:
<!--- if either session.user or session.user.somevar does not exist --->
<cfif NOT structKeyExists(session, 'user') or 
      NOT structKeyExists(session.user, 'somevar')>
...

----------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top