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

CFCs & CFInvoke

LEGEND ,
Jul 07, 2006 Jul 07, 2006

Copy link to clipboard

Copied

I am attempting to play with components and invoke them inside web pages. I
created a simple CFC (news.cfc) to pull the latest news story from the
database:

<cfcomponent>
<cffunction name="LatestNews" access="public" returntype="string">
<cfargument name="myArgument" type="string" required="false">
<cfquery name="news" datasource="#Request.MainDSN#">
SELECT News_ID, Story,"Date" FROM ms411.news ORDER BY "Date" DESC
</cfquery>
<cfset myResult="foo">
<cfreturn MyResult>
</cffunction>
</cfcomponent>

In the web page I want to display the results I have:

<cfinvoke
component="news"
method="LatestNews"
returnvariable="returnVar">
</cfinvoke>

Then I have, in the area I want to display it:

<cfoutput query="returnVar">
<tr>
<td>#Date#</td>
</tr>
<tr>
<td>#Story#</td>
</tr>
</cfoutput>

I keep getting this error:
Attribute validation error for tag cfoutput.
The value of the attribute query, which is currently "returnVar", is
invalid.

I have the Coldfusion MX Bible sitting next to me and (on page 452 for those
of you that have it) it give the same example. Same invoke, same 'cfoutput
query = "returnVar"''. Why am I getting this error and how do I fix it?

Thanks!

--
Wally Kolcz
Developer / Support


TOPICS
Advanced techniques

Views

547

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 ,
Jul 07, 2006 Jul 07, 2006

Copy link to clipboard

Copied

because you're CFC is returning a string and not a query?
<cffunction name="LatestNews" access="public" returntype="string">
HTH
--
Tim Carley
www.recfusion.com
info@NOSPAMINGrecfusion.com

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 ,
Jul 07, 2006 Jul 07, 2006

Copy link to clipboard

Copied

If you run this code:
<cfdump var="#ReturnVar#">

you get
foo

Right? Read your function and see if you can figure out why.

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 ,
Jul 07, 2006 Jul 07, 2006

Copy link to clipboard

Copied

I changed it to 'query' rather than string, but now I am getting:

The value returned from function LatestNews() is not of type query.

and it is directing me to:
returnvariable="returnVar">


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 ,
Jul 07, 2006 Jul 07, 2006

Copy link to clipboard

Copied

Because I am have <cfset myResult="foo">. That is what DW gave me to work
with. I didnt have it originally, but it was giving me an error for not
setting a variable.

What works in the book, isn't working in DW and using CF Server MX 7. I am
confused....


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 ,
Jul 07, 2006 Jul 07, 2006

Copy link to clipboard

Copied

As well as the returntype of your function being a string instead of a
query.
<cffunction name="LatestNews" access="public" returntype="string">

You are also not returning the query.
<cfquery name="news" datasource="#Request.MainDSN#">
...
<cfreturn MyResult>

That should be <cfreturn news> so that it returns the query you created.

You can remove the <cfargument ...> line, since you are not using it.
Unless you plan to add functionality later that would make use of the value.

To jump start you an better practices. You should var declare your news
variable so that it is local to the function and will not run into
problems with other variables named "news" in other places of your code.

<cfuntion ....>
<cfset var news = "">

Secondly you should pass in your dsn value to the function, not have the
function relay on a value existing in the calling code block. This
would then be a reason for a <cfargument ...> tag.

<cffunction ... returnType="query">
<cfargument name="DSN" requrired="true" ...>

<cfset var news = "">

<cfquery name="news" dataSource="#arguments.DSN#"...>
....
<cfreturn news>
</cffunction>

Your invoke then would add an argument parameter, there are two ways you
could do this.

> <cfinvoke
> component="news"
> method="LatestNews"
> returnvariable="returnVar"
dsn="#request.MainDSN#">
> </cfinvoke>

OR

> <cfinvoke
> component="news"
> method="LatestNews"
> returnvariable="returnVar">
<cfinvokeArgument name="dsn" value="#request.MainDSN#">
> </cfinvoke>

Wally Kolcz wrote:
> I am attempting to play with components and invoke them inside web pages. I
> created a simple CFC (news.cfc) to pull the latest news story from the
> database:
>
> <cfcomponent>
> <cffunction name="LatestNews" access="public" returntype="string">
> <cfargument name="myArgument" type="string" required="false">
> <cfquery name="news" datasource="#Request.MainDSN#">
> SELECT News_ID, Story,"Date" FROM ms411.news ORDER BY "Date" DESC
> </cfquery>
> <cfset myResult="foo">
> <cfreturn MyResult>
> </cffunction>
> </cfcomponent>
>
> In the web page I want to display the results I have:
>
> <cfinvoke
> component="news"
> method="LatestNews"
> returnvariable="returnVar">
> </cfinvoke>
>
> Then I have, in the area I want to display it:
>
> <cfoutput query="returnVar">
> <tr>
> <td>#Date#</td>
> </tr>
> <tr>
> <td>#Story#</td>
> </tr>
> </cfoutput>
>
> I keep getting this error:
> Attribute validation error for tag cfoutput.
> The value of the attribute query, which is currently "returnVar", is
> invalid.
>
> I have the Coldfusion MX Bible sitting next to me and (on page 452 for those
> of you that have it) it give the same example. Same invoke, same 'cfoutput
> query = "returnVar"''. Why am I getting this error and how do I fix it?
>
> Thanks!
>

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 ,
Jul 07, 2006 Jul 07, 2006

Copy link to clipboard

Copied

I get without something.
Variable MyResult is undefined.

In the CF MX Bible, all they have for a component is (minus the long stuff):

<cf component>
<cffunction name="GetCompany" returntype="query">
<cfargument..>
<cfquery...>
SQL
</cfquery>
<cfreturn companyRec>
<cfcomponent>

At no point in their examples do they set a variable for the cfreturn, which
is giving me an error if I don't.

I am trying to learn this.


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 ,
Jul 07, 2006 Jul 07, 2006

Copy link to clipboard

Copied

<cfset myResult="foo">
<cfreturn MyResult>
you're returning a string...name you're query "MyResult", remove the
cfset...
HTH
--
Tim Carley
www.recfusion.com
info@NOSPAMINGrecfusion.com

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 ,
Jul 07, 2006 Jul 07, 2006

Copy link to clipboard

Copied

At no point in their examples do they set a variable for the cfreturn,
which is giving me an error if I don't.

I suspect if you look at the cfquery tag, it has ...Name="companyRec".
This sets the query recordset to the variable 'companyRec' that is
returned in the <cfreturn ...> statement.


Wally Kolcz wrote:
> I get without something.
> Variable MyResult is undefined.
>
> In the CF MX Bible, all they have for a component is (minus the long stuff):
>
> <cf component>
> <cffunction name="GetCompany" returntype="query">
> <cfargument..>
> <cfquery...>
> SQL
> </cfquery>
> <cfreturn companyRec>
> <cfcomponent>
>
> At no point in their examples do they set a variable for the cfreturn, which
> is giving me an error if I don't.
>
> I am trying to learn this.
>
>

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 ,
Jul 07, 2006 Jul 07, 2006

Copy link to clipboard

Copied

LATEST
Thanks a ton Ian. That makes a lot more sense to me. I got it to work!


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