开发者

How can a cfparam'd variable be undefined?

A lot of sources call a single resource, typically through <cfthread ..>, but some use <cfinclude ..>.

Ideally, the code looks for the variable previous_state. If some variants are passed, then the resource will attempt to use them.

I received this error:

Variable PREVIOUS_STATE is undefined.

The line record points to the <cfif ..> in this chunk of code.

<cfparam    name=       "previous_state"
            default=    "" />

<cfif   isSimpleValue( previous_state )
        and len( previous_state ) eq 0>
    <cfset  previous_state= previousState />

</cfif>

My question is how can previous_state be undefined?

I can duplicate it in the application, but it's a fairly complex chain of code using threads. Perhaps the reference was eaten by the garbage collector?

I'm having trouble duplicating it in a simple code segment. I've tried setting the variable to the return of a function with returnType= "void", but <cfparam ..> seems to reset it to an empty string.

Here's the full code context. I removed the unrelated vars and such.

// Page
oComponent.foo();

// Component.foo()
<cfset  var local=  {
    previous_state= QueryNew( "foo" , "varchar" )

} />

<cfthread   name=   "foo_#createUUID()#"
            previousState=  "#local.previous_state#">
    <!---   Module does unrelated things... --->
    <cfmodule   template=   "some_module.cfm">
        <cfoutput>
        // unrelated things

        <cfparam    name=       "previous_state"
                    default=    "" />

        <!--- Next line is throwing error. --->
        <cfif   isSimpleValue( previous_state )开发者_开发百科
                and len( previous_state ) eq 0>
            <cfset  previous_state= previousState />

        </cfif>

        </cfoutput>

    </cfmodule>

</cfthread>

I'm now thinking cfparam is trying to use a scope that no longer exists by the time this code executes.


As the code is within a CFTHREAD tag I thing you should be passing previous_state as a CFTHREAD attribute, such as:

<cfparam name="previous_state" default="" />

    <CFTHREAD previous_state = previous_state
    previousState=  "#local.previous_state#">

    <cfif  isSimpleValue( ATTRIBUTES.previous_state  ) ........

    </CFTHREAD>

To quote the CF Docs:

"The Attributes scope contains attributes that are passed to the scope, and is available only within the thread and only for the life of the thread."


For previous_state to be undefined it would need to be null. You would need to do something like

<cfif isNull(previous_state)>true</cfif>

To prove this try the following

<cfset previous_state = "" />
<!--- Change to set previous_state --->
<cfset previous_state = javacast( "null", previous_state ) />

<cfparam name="previous_state" default="" />

<cfif isSimpleValue( previous_state ) and len( previous_state ) eq 0 >
    <cfset previous_state = previousState />
</cfif>


Make sure you scope your variables properly and your example above has a typo in it. I'm not having an issue running this code on both ACF9.0.1 and Railo 3.3.1.000

<cfparam    name=       "previous_state"
            default=    "" />

<cfdump var="#previous_state#">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜