URL variable issues in Lucee
We are switching from ColdFusion to Lucee for some of the sites, and we are facing various small issues. This is one of them. Take a look at such a code:
<cfquery name="getMenu" datasource="myDatabase"> SELECT id, url, name FROM menuItems <cfquery> <cfoutput query="getMenu"> <a href="#url#" id="#id#">#name#</a> </cfoutput>
And suddenly you receive this error:
Can't cast Complex Object Type Struct to String
Why? Is my query returning URL as a struct? No, the reason is in the scope handling. In ColdFusion, in such a case (looping through the query in cfoutput or cfloop) the default scope is the query. So, when I use #url# I’m in fact calling #getMenu.url#. On the contrary, in Lucee, the request scopes (form, URL, CGI, request) are handled first. So, the #url# is trying to cast URL scope as string hence the error.
In this case, you shoudl simply use the full scope. In the example below, I used it for all variables to be consistent. In fact, it is better practice to always use scope information in such cases. Nested loops variables can bite you 🙂
<cfquery name="getMenu" datasource="myDatabase"> SELECT id, url, name FROM menuItems <cfquery> <cfoutput query="getMenu"> <a href="#getMenu.url#" id="#getMenu.id#">#getMenu.name#</a> </cfoutput>