Skip to main content
WolfShade
Legend
December 5, 2017
Answered

Odd variable behavior

  • December 5, 2017
  • 1 reply
  • 824 views

Hello, all,

I've got another head-scratcher.  The following code is producing an error:

     thisField = trim(form.thatField);

     thisField = new components.myCFC().stripScript(variables.thisField);

     form.thatField = variables.thisField;

Now, according to cfdocs.org, if you do not explicitly declare that a variable is to be saved in any scope, then the VARIABLES scope is the default scope that the variable will be saved in (ie, "thisVar = '';" is the same as "variables.thisVar = '';".)

So.. when I typed thisField = trim(form.thatField); then it should have default saved to the VARIABLES scope.  But the second line is generating the error:

Element THISFIELD is undefined in VARIABLES.

???   Is this a bug?  Am I missing something?

V/r,

^ _ ^

    This topic has been closed for replies.
    Correct answer nic_tunney

    Depending on your version, try the THIS scope.  In a CFC though, you'll want to var so it stays local to just that function, or you can have problems.  Lot's of unexpected, hard to troubleshoot problems.

    var myVar = ...

    or

    <cfset var myVar = ... />

    -Nic

    1 reply

    Inspiring
    December 5, 2017

    Is this in a CFC or a CFM?

    Also, is myCFC.stripScript returning null?  That could also cause this.  Do you get the same error if you use thisField instead of variables.thisField (I assume you will).  Also, is the error occurring on line 2 or 3?  I bet it is line 3.

    Might want to move myCFC into a persistent scope as a singleton (such as application, or if you use something like Wirebox) as this might get heavy creating new objects each time you want to strip out characters in your app.

    -Nic

    WolfShade
    WolfShadeAuthor
    Legend
    December 5, 2017

    Hi, nic,

    This is in a CFC, a function that receives a form submit via AJaX, and is part of the form validation/sanitization process.

    myCFC().stripScript() is not returning null.  I'll try removing the variables. from thisField to see if that triggers anything.

    ...

    No, it didn't trigger the same alert, but here's the weird thing:  immediately below my sample code is more code just like it, but testing after removing variables. from thisField triggered another error - it skipped several sections exactly like my sample code, and found another similar situation.

    if(len(trim(variables.returnCI))){

    returnResult &= "blah blah correct this field";

    }

    But now it says that RETURNCI is undefined in VARIABLES.  Kind of inconsistent, seeing as it skipped the 5 just like it before it got to returnCI.

    We are not using any kind of MVC like Wirebox or Fusebox.  Straight CF.

    V/r,

    ^ _ ^

    nic_tunneyCorrect answer
    Inspiring
    December 5, 2017

    Depending on your version, try the THIS scope.  In a CFC though, you'll want to var so it stays local to just that function, or you can have problems.  Lot's of unexpected, hard to troubleshoot problems.

    var myVar = ...

    or

    <cfset var myVar = ... />

    -Nic