Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a cfparam'd variable be undefined?

Tags:

coldfusion

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.

like image 849
Bradley Moore Avatar asked Jun 17 '26 15:06

Bradley Moore


2 Answers

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."

like image 199
Barry Jordan Avatar answered Jun 20 '26 05:06

Barry Jordan


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>
like image 25
Andy Jarrett Avatar answered Jun 20 '26 04:06

Andy Jarrett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!