Skip to main content
WolfShade
Legend
Dec 5, 2017 6:03 PM UTC
Answered

Odd variable behavior

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 is closed to new replies. Start a new post to keep the conversation going.
    nic_tunneyCorrect answer
    Inspiring
    Dec 5, 2017 6:34 PM UTC

    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

    7 replies

    Inspiring
    Dec 5, 2017 6:15 PM UTC

    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

    Translate
    WolfShade
    WolfShadeAuthor
    Legend
    Dec 5, 2017 6:28 PM UTC

    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,

    ^ _ ^

    Translate
    nic_tunneyCorrect answer
    Inspiring
    Dec 5, 2017 6:34 PM UTC

    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

    Translate