Why a parameter is not defined, if I can dump it a

2019-09-04 11:30发布

Just a little clueless... using Coldfusion8, if I dump my session to file:

<cfdump output="D:\ColdFusion8\logs\dump.txt" var="#Session#">

this includes:

accounttyp: whatever

I get the same result if I only dump this parameter:

<cfdump output="D:\ColdFusion8\logs\dump.txt" var="#Session.accounttyp#">

Question:
If it's defined and dump-able, how come checking isDefined like so:

<cfdump output="D:\ColdFusion8\logs\dump.txt" var="#IsDefined(Session.accounttyp)#"> 

turns out to be NO? If it's there it should be defined, shouldn't it?

Thanks for some clarification.

2条回答
叼着烟拽天下
2楼-- · 2019-09-04 11:33

<cfdump output="D:\ColdFusion8\logs\dump.txt" var="#IsDefined(Session.accounttyp)#">

It is because the syntax is incorrect. IsDefined expects the name of a variable ie a string. By omitting the quotes around the variable name, the session variable gets evaluated first, and its value ("whatever") is what gets passed into IsDefined. So the code is actually checking for a variable named "whatever", not "session.accounttyp" ie:

    <cfif IsDefined("whatever")>

That is why the result is NO. This is the correct syntax. (Notice the quotes and lack of pound signs).

    <cfif IsDefined("Session.accounttyp")>

However, I would suggest switching to structKeyExists. It is generally preferred over IsDefined because it is more precise.

查看更多
萌系小妹纸
3楼-- · 2019-09-04 11:42

This returns a boolean value:

#IsDefined(Session.accounttyp)#

So, you are asking it to return yes or no.

A better test might be this:

<cfif isDefined("Session.accounttyp")>
    <cfdump output="D:\ColdFusion8\logs\dump.txt" var="#Session.accounttyp#">
</cfif>
查看更多
登录 后发表回答