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

Variable "varName" undefined when trying to call a function

LEGEND ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

Hello, all,

I was just shown that an application written by someone else over ten years ago is working fine under CF9 and CF10; but it's breaking in CF11.

I have something like:

<cfinclude template="../myComponent.cfc" />

Now, in myComponent.cfc, I have a function:

<cffunction name="getThisAttribute" returntype="string" output="no">

  <cfargument name="part1" /><cfargument name="part2" />

  ...

  <cfreturn thisValue />

</cffunction>

Under CF11, in the first page (again, this works in CF9 and CF10), if I do the following:

<cfset thisVar = getThisAttribute(part1,'part2') />

I get "getThisAttribute undefined".  Am I going to have to completely re-write how all this works if we switch to CF11???

V/r,

^_^

Views

381

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
Community Expert ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

Not quite kosher code, but it should work. What about:

<cfset thisVar = getThisAttribute('part1','part2') />

or

<cfset thisVar = getThisAttribute(1,2) />

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 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

Thanks for the suggestion, BKBK​, but getThisAttribute is a function in the CFC that is CFINCLUDEed prior to the CFSET; but CF is convinced that getThisAttribute is not defined.  It's not the arguments that are preventing anything - it just doesn't exist.

I thought perhaps that it might be because the CFC is CFINCLUDE, not a component that is mapped and accessed via <cfset thisVar = new components.getThisAttribute(part1,'part2') />, but that is weird since it works in CF9/10.

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
Advocate ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

To me this looks like a bug in CF9 & 10 that you unintentionally exploited. The getThisAttribute appears to be defined as a function of the myComponent and nowhere are you reverencing a myComponent instance. Now if your first module was a cfc and you used the extends="myComponent" parameter, then your cfset call should work -- although on occasion I have had to explicitly state this.getThisAttributes(...) for unknown reasons.

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 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

I'll give the extends="myComponent" a shot.  Thanks!

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
Community Expert ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

WolfShade wrote:

... but CF is convinced that getThisAttribute is not defined. It's not the arguments that are preventing anything - it just doesn't exist.

Coldfusion may see a function with wrong or different arguments as different from the function in the component. If after making the changes I suggested you still get the error then verify whether the path .../myComponent.cfc is correct.

In any case, as everyone has said, you should just correct the code. Either remove the cfcomponent tag and revert to CFMs, or define the CFC in a separate file and access the function via createobject("component","path-to-component").functionName().

Remember, there is one good reason to do the right thing. If you don't, those who inherit the code from you will blame it on you.

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 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

BKBK wrote:

In any case, as everyone has said, you should just correct the code. Either remove the cfcomponent tag and revert to CFMs, or define the CFC in a separate file and access the function via createobject("component","path-to-component").functionName().

Just curious - I prefer <cfset varName = new component.path.to.cfc.functionName(args) /> when the this.mappings is used.  Is there a benefit to using createObject(), instead?

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
Community Expert ,
Dec 10, 2016 Dec 10, 2016

Copy link to clipboard

Copied

LATEST

WolfShade wrote:

Just curious - I prefer <cfset varName = new component.path.to.cfc.functionName(args) /> when the this.mappings is used. Is there a benefit to using createObject(), instead?

Creating a component by means of createobject or new amounts to the same thing. However, I would separate the creation of the object from the function call. That frees the object up for reuse.

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
Advocate ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

That's definitely not how CFC's are supposed to be used.

I suspect the quickest solution would be to rename the CFC file to a CFM file and do a search and replace in all scripts that include the file.

Cheers

Eddie

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 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

That's definitely not how CFC's are supposed to be used.

Oh, I know.  I inherited this code, it was written over ten years ago (round about CF MX6 or MX7, methinks), and I'm personally hoping that it is so old, outdated, and just plain vulnerable that the client will agree to just deleting the whole thing, possibly writing another from scratch.

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
Resources
Documentation