Skip to main content
WolfShade
Legend
June 4, 2012
Question

Variable being deleted inside of a cffunction within a cfcomponent

  • June 4, 2012
  • 1 reply
  • 498 views

Hello, everyone.

Scratching my head over this one.

I'm working on a project that will print a certificate after the user has clicked a link to view a training module.  Unfortunately, this isn't on a server that anyone can access, and I can't copy/paste code because my development system is on a different network that doesn't have internet access.    So, I'll do my best to get as much information as I can into this question.

When the "Print Cert" button is clicked, the page is redirected to the print_c.cfm page that also has a URL parameter (ie "print_c.cfm?tid=xx" where xx is an integer.)  Within this page is a cfinvoke that calls a function that will INSERT a new record into a table indicating that the user has completed the training, and then will SELECT said record to provide information for the printed certificate.

<cffunction name="setInfo" returntype="query" output="true">

  <cfargument name="user" type="string" require="yes">

  <cfargument name="tid" type="string" require="yes">

        <!--- as of this point, the variable "user" exists and is used --->

  <cfoutput>#user#<br /></cfoutput> <!--- NO ERROR --->

  <cfquery>  [ insert statement ]</cfquery>

        <!--- as of this point, the variable "user" no longer exists --->

  <cfoutput>#user#<br /></cfoutput> <!--- ERROR THROWN HERE --->

  <cfquery>  [ select statement ]</cfquery>

  <cfreturn this_data>

</cffunction>

Somehow, after the first query is run the variable "user" disappears.  Any suggestions on what might be causing this greatly appreciated.

^_^

This topic has been closed for replies.

1 reply

Sean Coyne
Participating Frequently
June 4, 2012

Well its hard to tell because you don't show the code that actually calls this function, but I would suggest that you scope your variables properly.

I assume your cfquery tag has a "name" attribute, but I don't see a <cfset var qNameOfQuery = "" /> at the top of the function to properly var scope that query to the function.  Now, its doubtful that has anything to do with disappearing variables, however improper scoping leads to very weird bugs.

Use the VarScoper tool and fix invalidly scoped variables.

Next, you should be referencing the "user" variable as arguments.user because it is in the function's arguments scope.  There could be another variable in the CFC's variables scope called user and you could be accessing a different variable when you think you are accessing the arguments scope.  ColdFusion will search the arguments scope first, so it should find the user variable in the arguments scope, but IMO its better to be overly verbose and explicitly identify the scope you want rather than try to track down weird bugs like this.

Again, var scope and use the proper scope of variables that you access.

Also, if you can show the calling code and any other code that affects this then perhaps we can help, but short of that, this is the best I can suggest.

WolfShade
WolfShadeAuthor
Legend
June 4, 2012

Thank you, Sean.  I'll do as you suggest and report back.

^_^