frequent
frequent

Reputation: 28513

In the following Coldfusion snippet, when can my variable x be undefined?

Odd question... My application (coldfusion8/MySQL 5.0.88) runs fine except when a user tries to access it using IE6. This throws the following error:

 Element PL_SELLERS is undefined in VARIABLES

I'm having trouble reproducing, as it all browsers I can test (IE8 including) do not throw this error.

I know the problem is in the following snippet:

<cfif Session.reload_user EQ "true" OR ( Len(Session.pricelists) EQ 0 OR Len(Session.pl_sellers) EQ 0)>
    <cfquery datasource="#Session.datasource#" name="q_preislisten">
        ... query ...
    </cfquery>
    <cfif q_preislisten.recordcount NEQ 0>
        <cfset variables.pl_sellers = "">
        <cfloop query="q_preislisten">
            <cfset variables.pl_sellers = variables.pl_sellers & q_preislisten.iln_verkaeufer & ',';>
        </cfloop>
    </cfif>
    <cfif len(variables.pl_sellers) NEQ 0 )>
        <cfset variables.pl_sellers = Left(variables.pl_sellers, len(variables.pl_sellers)-1)>
        <cfset Session.pl_sellers = variables.pl_sellers>
        <cfset Session.reload_user = "false">
    </cfif>
<cfelse>
    <cfset variables.pl_sellers = Session.pl_sellers>
</cfif>

So my question:
Under what circumstances can variables.pl_sellers be undefined?

I will move the inital declaration <cfset variables.pl_sellers = ""> outside of the whole if statement, so it will always be at least an empty string. Another reason I can think of the check for len(variables.pl_sellers) being outside of the recordcount-if statement. Are there any other things I'm missing?

Thanks!

Upvotes: 0

Views: 140

Answers (3)

Miguel-F
Miguel-F

Reputation: 13548

Gotta love IE6! Not really an answer to your question but I would add IsDefined() checks so you can at least handle the error gracefully.

<cfif Session.reload_user EQ "true" OR ( Len(Session.pricelists) EQ 0 OR Len(Session.pl_sellers) EQ 0)>
    <cfquery datasource="#Session.datasource#" name="q_preislisten">
        ... query ...
    </cfquery>
    <cfif q_preislisten.recordcount NEQ 0>
        <cfset variables.pl_sellers = "">
        <cfloop query="q_preislisten">
            <cfset variables.pl_sellers = variables.pl_sellers & q_preislisten.iln_verkaeufer & ',';>
        </cfloop>
    </cfif>
    <cfif IsDefined("variables.pl_sellers") AND len(variables.pl_sellers) NEQ 0 )>
        <cfset variables.pl_sellers = Left(variables.pl_sellers, len(variables.pl_sellers)-1)>
        <cfset Session.pl_sellers = variables.pl_sellers>
        <cfset Session.reload_user = "false">
    <cfelse>
        <!--- handle the error here --->
    </cfif>
<cfelse>
    <cfset variables.pl_sellers = Session.pl_sellers>
</cfif>

Upvotes: 1

Busches
Busches

Reputation: 1964

If your preislisten.recordcount is 0, then when it checks the next IF block it'll be undefined.

Upvotes: 3

snake
snake

Reputation: 762

As above, if query returns no rows then the.variable does not get created.

Upvotes: 0

Related Questions