Skip to main content
Inspiring
Nov 7, 2006 5:11 PM UTC
Question

my fisrt cfc

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 is closed to new replies. Start a new post to keep the conversation going.

5 replies

Inspiring
Nov 7, 2006 6:54 PM UTC
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">





Translate
Participant
May 24, 2018 2:50 AM UTC

why my code is still not working?

can you help me please

Translate
mega_LAuthor
Inspiring
Nov 7, 2006 5:54 PM UTC
I got it!! thank you for pointing that out ! Whew!
Translate
Inspiring
Nov 7, 2006 5:46 PM UTC
Your function doesn't return anything. I don't see a cfreturn tag anywhere.
Translate
Inspiring
Nov 7, 2006 5:53 PM UTC
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>


Translate