Lee
Lee

Reputation: 999

Coldfusion CFC - The value returned from the function is not of type query?

Rather late to the party, I'm trying to move across to using CFCs in an effort to streamline things. At this stage, I'm simply trying to find my feet and understand them - using CFWACK 9 as a guide.

However, my first attempt has me stumped!

Here's 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 calling .cfm page;

<cfparam name="blogid" default="24" >

<cfinvoke component="td" 
      method="printBlogEntries" 
      searchString="#blogid#" 
      returnvariable="blogentries" >

<cfoutput>#blogentries#</cfoutput>

The error returned is;

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

If I manually run the query and do a cfdump, everything looks as it should, so I'm clearly doing something wrong in the cfc. However, the way it is now is pretty much a replica of the example given in the CFWACK book.

Pointers would be much appreciated (as would any recommended reading on the subject!)

Upvotes: 8

Views: 7621

Answers (1)

Adrian J. Moreno
Adrian J. Moreno

Reputation: 14859

In your function "listBlogEntries", you have a query named "getBlogEntries". You're getting the error because this function isn't returning anything at the moment. Just add a cfreturn after the query.

Also, if you're on ColdFusion 9 or better, you can do away with <cfset var getBlogEntries = ""> and just use the function local variable scope "local". It does the same thing.

<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, he's using two functions in his component because odds are, multiple other functions need the query generated by listBlogEntries().

Upvotes: 11

Related Questions