I have been having the hardest time trying to resolve the weirdest error when any kind of condition is used within any kind of loop.
The error I keep getting is: "Complex object types cannot be converted to simple values." on the line that contains <cfloop condition="true">
. What's even funnier is that a <cfbreak>
doesn't break out of the loop. I have used the debugger within Eclipse to confirm that this code is actually executed.
The odd thing is sometimes the code loops forever even though I know the <cfbreak>
gets executed and sometimes it errors out on the first iteration.
I've confirmed that this issue occurs on two other computers as well.
Before everyone freaks out on my use of the always true condition, I used to have a valid condition: <cfloop condition="not done">
where done was a boolean. This caused the same error.
I'm working on older code written by someone else, so rewriting isn't an option at this time.
I have just patched ColdFusion 8.0.1 to the latest updater 4 to see if that would resolve the issue and it hasn't.
This is what the code essentially looks like:
<cfloop condition="true">
<cfif condition is true>
<cfbreak>
</cfif>
</cfloop>
Here is the code that does get executed:
<cfset done = false />
<cfloop condition="true">
<cfscript>
try
{
lineitem.quantity = quantityRemaining;
quantityRemaining = 0;
arrayAppend( currentTub, lineitem );
currentCartItemCount = currentCartItemCount + lineitem.quantity;
currentTubItemCount = currentTubItemCount + lineitem.quantity;
}
catch(Any e){}
availableSpaceInCart = this.itemsPerCart - currentCartItemCount;
availableSpaceInTub = this.itemsPerTub - currentTubItemCount;
</cfscript>
<cfif quantityRemaining LTE 0>
<cfset done = true />
<cfbreak />
</cfif>
</cfloop>
UPDATE:
Well we figured out what the issue was. There was two <CFOUTPUT>
tags wrapped around this loop, the outer <CFOUTPUT query='query_name' groupby='column_name'>
tag was using a QueryNew()
generated query which for some reason ColdFusion didn't like. So we resolved that issue and it resolved our weird issue.
You are passing ambiguous values to a conditional loop.
When you write:
<cfloop condition="x">
You are telling ColdFusion: continue to loop so long as 'x' evaluates to true. This could be a statement like (myvar gte 10), which would evaluate to true if myvar is greater than or equal to 10. If some mechanism inside the loop decrements myvar, eventually, a pass through the loop will cause it to stop, because myvar will no longer be greater than or equal to 10 (it has dropped below 10).
Now, let's look at your condition:
<cfloop condition="true">
Is CF considering it as:
<cfif 'true'>
Or
<cfif TRUE>
One is definitely wrong, and will throw an error, if you write into a ColdFusion statement. You will probably find that you cannot evaluate a "string" into a boolean TRUE or FALSE evaluation. I'm almost certain the parsing of a string value is confusing ColdFusion with the actual boolean TRUE or FALSE value. It is perhaps this lexical conversion of 'true' to TRUE that is causing your object-related errors. What you want, more than likely is:
since the value of done is set to FALSE to start, which, when coupled with a logical NOT, evaluates to true.
Then, later down your statement:
<cfif quantityRemaining LTE 0>
<cfset done = true />
</cfif>
There is no need for a <cfbreak>
statement, which forcibly breaks you out of a loop. If you are conditionally looping, the evaluation of that variable will affect the iteration of the loop (read: it will stop it).
Furthermore, you may very well want to change your condition to:
<cfloop condition="quantity LTE 0">
and will not require the done variable at all.