Reputation: 5213
Consider the following code:
<cfset result.enrollments = {} />
<cfset result.enrollments = getCorrectionList(SESSION.id,SESSION.term) />
<cfdump var="#result#" /><Cfabort/>
<cffunction name="getCorrectionList">
<cfargument name="id" required="true" type="string" />
<cfargument name="term" required="true" type="numeric" default="#getCurrentSemester().code#" />
<cfset result = {} />
<cfset result.status = 500 />
<cfset result.message = 'Unknown Error' />
<cfhttp url="THERE IS A REAL URL HERE" />
<cfif cfhttp.statusCode EQ '200 OK'>
<cfset courses = deserializeJson(cfhttp.fileContent,false) />
<cfif courses.recordCount EQ 0>
<cfset result.message = 'You are not currently enrolled in any courses for #ARGUMENTS.term#' />
<cfdump var="#result#" />
<cfreturn result />
</cfif>
<!--- MORE STUFF --->
Now when this runs I get an single struct output with two keys message
and status
. This is from the dump inside of the getCorrectionList
function.
I then get a second struct output with the keys enrollments
, message
, and status
. Inside the enrollments
key is another struct with enrollments
, message
, and status
. Inside that enrollments
key is another struct with the same keys and so on 50 times with the last struct being empty.
Seems like some recursive actions is going on but where/how?
I have no idea what is going on. As you can see from my code there are no loops. I know the URL resolves correctly and it returns a query and has a recordcount. I can see the data dump at the right spots. But how can the #result#
in the function show a single struct but the #result#
outside the function show a 50 deep struct repeating itself. It doesn't make any sense.
Upvotes: 2
Views: 75
Reputation: 29870
I dunno why neither Leigh or Scott actually made their comments answers, but they're both right.
You have this reference to result
outside your function:
<cfset result.enrollments = getCorrectionList(SESSION.id,SESSION.term) />
And at the end of your function you do this:
<cfreturn result />
Which in effect means you're doing this:
<cfset result.enrollments = result />
Meaning result.enrollments
is a reference back to its own parent result
.
And <cfdump>
is duly displaying the circular reference.
As they both said, you need to localise your variables inside a function, either by var
ing them:
<cfset var result = {} />
Or explicitly putting them in the local
scope:
<cfset local.result = {} />
That will make the function's result
be a discrete variable, not simply another reference to the calling code' result
variable.
You should always localise your function variables, unless you specifically mean to be referencing a calling-code variable, in which event it makes you code clearer if you explicitly scope it to make it obvious what you mean, eg:
<cfset variables.result = {} />
But this is not what you are meaning to do here. Localise your function's variables.
Upvotes: 2