Skip to main content
Inspiring
November 7, 2006
Question

my fisrt cfc

  • November 7, 2006
  • 3 replies
  • 887 views
I tried to learn to use cf component by creating a simple cfc.
All I want to do within the cfc is to provide 1 query so within the application this query doesn't have to be created many times.

I created a getFrmTypes.cfc this way:
<!--- getFrmTypes.cfc --->
<CFCOMPONENT>
<CFFUNCTION Name="FrmTypes" ReturnType="query">
<cfquery name="getTypes" datasource="MyDB">
Select * from TblForms order by FormID Asc
</cfquery>
</CFFUNCTION>
</CFCOMPONENT>

Then on the CF template that is calling this cfc I wrote:
<CFINVOKE Component="getFrmTypes" Method="FrmTypes" ReturnVariable="FormTypesQuery">

When I run this template I got this error:
The value returned from function FrmTypes() is not of type query.
If the component name is specified as a return type, the reason for this error might be that a definition file for such component cannot be found or is not accessible.
line 12

12 : <CFINVOKE Component="getFrmTypes" Method="FrmTypes" ReturnVariable="FormTypesQuery">
13 :
14 : <table border="0">

I don't understand what is the meanng of this error and how can I make correction, please help.

This topic has been closed for replies.

3 replies

Inspiring
November 7, 2006
I added: <CFRETURN FrmTypes> and run the app. again, still get the same
error.

In your example you don't want to return the function, you want to
return the query you generated in the function.

<cfreturn getTypes>


That will remove the error you are receiving but the function will not
be doing what you want. The way it is written it will still run the
query every time the function is called to get the data. To do what you
stated, not have the query created many times, you are going to want to
have one function (or in the component pseudo constructor) create the
query and store it, then another function to read it back to any code
that needs to use it. You will then need to store the initiated
component in some type of persistent variable so that the query is
persisted and does not get re-run unnecessarily.

<CFCOMPONENT>
<CFFUNCTION Name="set_FrmTypes" ReturnType="void">
<cfset var getTypes = ""> <!--- The var keyword initilizes the
getTyes variable to be local to this function to prevent problems. --->

<cfquery name="getTypes" datasource="MyDB">
Select * from TblForms order by FormID Asc
</cfquery>

<cfset variables.types = getTypes> <!--- This sets the query to an
internal variable of the component/object--->
</CFFUNCTION>

<cffunction name="get_frmTypes" returnType="query">
<cfreturn variables.types> <!--- This returns the query from the
internal variable--->
</cffunction>
</CFCOMPONENT>


Then in your calling code.
<!--- This invokes the component calling the set_frmTypes function to
initilize the query and stores the result in the application scope so it
can be used anywhere in the given application --->
<CFINVOKE Component="getFrmTypes" Method="set_FrmTypes"
ReturnVariable="application.FormTypesQuery">

<!--- The calls the above saved component and gets the query out of it. --->
<CFINVOKE Component="#application.FromTypesQuery#" Method="get_frmTypes"
ReturnVariable="theQuery">





Participant
May 24, 2018

why my code is still not working?

can you help me please

mega_LAuthor
Inspiring
November 7, 2006
I got it!! thank you for pointing that out ! Whew!
Inspiring
November 7, 2006
Your function doesn't return anything. I don't see a cfreturn tag anywhere.
Inspiring
November 7, 2006
As Dan stated, CFfuntions need to have a cfreturn:

<CFCOMPONENT>
<CFFUNCTION Name="FrmTypes" ReturnType="query">
<cfquery name="getTypes" datasource="MyDB">
Select * from TblForms order by FormID Asc
</cfquery>
<cfreturn getTypes> <-- added ---
</CFFUNCTION>
</CFCOMPONENT>