• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Odd variable behavior

LEGEND ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

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,

^ _ ^

Views

622

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Dec 05, 2017 Dec 05, 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

Votes

Translate

Translate
Engaged ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

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,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

Will "var myVar='foobar';" put it in the variables scope?  Or just keep it local?

V/r,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

Local, and from the looks of the few lines of code you provided that is what you'd want.  You can put things into the variables scope by using cfproperty or variables.myVar if you need private variables that can be used across all functions (depending on your version/flavor will define whether cfproperty creates in this or variables).  This needs to be thought out so you are not introducing a race condition or other undesirable condition.  Use local whenever possible in CFC functions unless you truly need a shared variable.  Also, the the THIS scope allows the property to be shared outside the component without a getter/accessor method (public property), just for completeness.

-Nic

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

Thank you, nic.  I guess I'll be going through hundreds of lines of code to put it in the local scope. 

V/r,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

LATEST

Let me suggest a great tool to use before deploying code: GitHub - mschierberl/varscoper: varScoper is a code scanning tool that can be used to identify varia...

This will give you line numbers and files for any variables in CFCs that are not properly scoped.  Things like query names, loop iterators, etc can sometimes be missed.

-Nic

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation