Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

coldfusion

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

like image 510
Lee Avatar asked Mar 13 '14 00:03

Lee


People also ask

What is cf function?

Usage. The cffunction tag can define a function that you call in the same manner as a ColdFusion built-in function. To define a ColdFusion component (CFC) method, use a cffunction tag. The following example shows cffunction tag attributes for a simple CFC method that returns a ColdFusion Query object.

How do you call a function in ColdFusion?

You want define a variable by calling a method in this object (CFC). The value of list is based on whatever was returned by the function doSomething , which is a public function inside the component com. Test that exists in the application variable.

How do you call CFCS?

You can invoke CFC methods directly by specifying the CFC in a URL, or by using HTML and CFML form tags.


1 Answers

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().

like image 119
Adrian J. Moreno Avatar answered Sep 18 '22 18:09

Adrian J. Moreno