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!
>