Coldfusion CFC - the value returned by the function is not of the request type?

Rather late to the party, I'm trying to switch to using CFCs to make things easier. At this point, I'm just trying to find my legs and understand them - using CFWACK 9 as a guide.

However, my first attempt silenced me!

Here is what I have in my CFC;

<cffunction name="listBlogEntries" returntype="query" output="false" access="remote" hint="Find all blog entries by blogid, sorted by id desc"> <cfargument name="blogid" required="true" default="24"> <cfset var getBlogEntries = ""> <cfquery name="getBlogEntries"> SELECT ID, entry_title FROM blog_entries WHERE blogID='#ARGUMENTS.blogid#' ORDER BY ID DESC LIMIT 10 </cfquery> </cffunction> <cffunction name="printBlogEntries" returntype="void" access="remote" hint="Lookup blog entries and return formatted"> <cfargument name="blogid" required="true" default="24"> <cfset var qBlogEntries = listBlogEntries(ARGUMENTS.blogid)> <cfoutput query="qBlogEntries"> <h1>Entry ID: #qBlogEntries.ID#</h1> <h3>Entry Title: #qBlogEntries.entry_title#</h3> </cfoutput> <cfreturn> </cffunction> 

And my .cfm page to call.

 <cfparam name="blogid" default="24" > <cfinvoke component="td" method="printBlogEntries" searchString="#blogid#" returnvariable="blogentries" > <cfoutput>#blogentries#</cfoutput> 

Error returned:

 The value returned from the listBlogEntries function is not of type query. 

If I manually run the request and do cfdump, everything looks as it should, so I am obviously doing something wrong in cfc. However, the way it is now is pretty much a copy of the example in CFWACK.

Pointers will be highly appreciated (like any recommended reading on this!)

+6
source share
1 answer

In your listBlogEntries function, you have a query called getBlogEntries. You get an error because this function does not return anything at the moment. Just add cfreturn after the request.

Alternatively, if you are working in ColdFusion 9 or higher, you can end with <cfset var getBlogEntries = ""> and just use the local variable scope "local". He does the same.

 <cffunction name="listBlogEntries" returntype="query" output="false" access="remote" hint="Find all blog entries by blogid, sorted by id desc"> <cfargument name="blogid" required="true" default="24"> <cfquery name="local.getBlogEntries"> SELECT ID, entry_title FROM blog_entries WHERE blogID = <cfqueryparam value="#ARGUMENTS.blogid#" cfsqltype="cf_sql_integer"> ORDER BY ID DESC LIMIT 10 </cfquery> <cfreturn local.getBlogEntries> </cffunction> 

And @Cory, it uses two functions in its component, because coefficients, several other functions require a query generated by listBlogEntries ().

+9
source

All Articles