9 Replies Latest reply on Jul 7, 2006 10:36 AM by Newsgroup_User

    CFCs & CFInvoke

    Level 7
      I am attempting to play with components and invoke them inside web pages. I
      created a simple CFC (news.cfc) to pull the latest news story from the
      database:

      <cfcomponent>
      <cffunction name="LatestNews" access="public" returntype="string">
      <cfargument name="myArgument" type="string" required="false">
      <cfquery name="news" datasource="#Request.MainDSN#">
      SELECT News_ID, Story,"Date" FROM ms411.news ORDER BY "Date" DESC
      </cfquery>
      <cfset myResult="foo">
      <cfreturn MyResult>
      </cffunction>
      </cfcomponent>

      In the web page I want to display the results I have:

      <cfinvoke
      component="news"
      method="LatestNews"
      returnvariable="returnVar">
      </cfinvoke>

      Then I have, in the area I want to display it:

      <cfoutput query="returnVar">
      <tr>
      <td>#Date#</td>
      </tr>
      <tr>
      <td>#Story#</td>
      </tr>
      </cfoutput>

      I keep getting this error:
      Attribute validation error for tag cfoutput.
      The value of the attribute query, which is currently "returnVar", is
      invalid.

      I have the Coldfusion MX Bible sitting next to me and (on page 452 for those
      of you that have it) it give the same example. Same invoke, same 'cfoutput
      query = "returnVar"''. Why am I getting this error and how do I fix it?

      Thanks!

      --
      Wally Kolcz
      Developer / Support


        • 1. Re: CFCs &amp; CFInvoke
          Level 7
          because you're CFC is returning a string and not a query?
          <cffunction name="LatestNews" access="public" returntype="string">
          HTH
          --
          Tim Carley
          www.recfusion.com
          info@NOSPAMINGrecfusion.com
          • 2. Re: CFCs &amp; CFInvoke
            Dan Bracuk Level 5
            If you run this code:
            <cfdump var="#ReturnVar#">

            you get
            foo

            Right? Read your function and see if you can figure out why.
            • 3. Re: CFCs &amp; CFInvoke
              Level 7
              I changed it to 'query' rather than string, but now I am getting:

              The value returned from function LatestNews() is not of type query.

              and it is directing me to:
              returnvariable="returnVar">


              • 4. Re: CFCs &amp; CFInvoke
                Level 7
                Because I am have <cfset myResult="foo">. That is what DW gave me to work
                with. I didnt have it originally, but it was giving me an error for not
                setting a variable.

                What works in the book, isn't working in DW and using CF Server MX 7. I am
                confused....


                • 5. Re: CFCs &amp; CFInvoke
                  Level 7
                  As well as the returntype of your function being a string instead of a
                  query.
                  <cffunction name="LatestNews" access="public" returntype="string">

                  You are also not returning the query.
                  <cfquery name="news" datasource="#Request.MainDSN#">
                  ...
                  <cfreturn MyResult>

                  That should be <cfreturn news> so that it returns the query you created.

                  You can remove the <cfargument ...> line, since you are not using it.
                  Unless you plan to add functionality later that would make use of the value.

                  To jump start you an better practices. You should var declare your news
                  variable so that it is local to the function and will not run into
                  problems with other variables named "news" in other places of your code.

                  <cfuntion ....>
                  <cfset var news = "">

                  Secondly you should pass in your dsn value to the function, not have the
                  function relay on a value existing in the calling code block. This
                  would then be a reason for a <cfargument ...> tag.

                  <cffunction ... returnType="query">
                  <cfargument name="DSN" requrired="true" ...>

                  <cfset var news = "">

                  <cfquery name="news" dataSource="#arguments.DSN#"...>
                  ....
                  <cfreturn news>
                  </cffunction>

                  Your invoke then would add an argument parameter, there are two ways you
                  could do this.

                  > <cfinvoke
                  > component="news"
                  > method="LatestNews"
                  > returnvariable="returnVar"
                  dsn="#request.MainDSN#">
                  > </cfinvoke>

                  OR

                  > <cfinvoke
                  > component="news"
                  > method="LatestNews"
                  > returnvariable="returnVar">
                  <cfinvokeArgument name="dsn" value="#request.MainDSN#">
                  > </cfinvoke>

                  Wally Kolcz wrote:
                  > I am attempting to play with components and invoke them inside web pages. I
                  > created a simple CFC (news.cfc) to pull the latest news story from the
                  > database:
                  >
                  > <cfcomponent>
                  > <cffunction name="LatestNews" access="public" returntype="string">
                  > <cfargument name="myArgument" type="string" required="false">
                  > <cfquery name="news" datasource="#Request.MainDSN#">
                  > SELECT News_ID, Story,"Date" FROM ms411.news ORDER BY "Date" DESC
                  > </cfquery>
                  > <cfset myResult="foo">
                  > <cfreturn MyResult>
                  > </cffunction>
                  > </cfcomponent>
                  >
                  > In the web page I want to display the results I have:
                  >
                  > <cfinvoke
                  > component="news"
                  > method="LatestNews"
                  > returnvariable="returnVar">
                  > </cfinvoke>
                  >
                  > Then I have, in the area I want to display it:
                  >
                  > <cfoutput query="returnVar">
                  > <tr>
                  > <td>#Date#</td>
                  > </tr>
                  > <tr>
                  > <td>#Story#</td>
                  > </tr>
                  > </cfoutput>
                  >
                  > I keep getting this error:
                  > Attribute validation error for tag cfoutput.
                  > The value of the attribute query, which is currently "returnVar", is
                  > invalid.
                  >
                  > I have the Coldfusion MX Bible sitting next to me and (on page 452 for those
                  > of you that have it) it give the same example. Same invoke, same 'cfoutput
                  > query = "returnVar"''. Why am I getting this error and how do I fix it?
                  >
                  > Thanks!
                  >
                  • 6. Re: CFCs &amp; CFInvoke
                    Level 7
                    I get without something.
                    Variable MyResult is undefined.

                    In the CF MX Bible, all they have for a component is (minus the long stuff):

                    <cf component>
                    <cffunction name="GetCompany" returntype="query">
                    <cfargument..>
                    <cfquery...>
                    SQL
                    </cfquery>
                    <cfreturn companyRec>
                    <cfcomponent>

                    At no point in their examples do they set a variable for the cfreturn, which
                    is giving me an error if I don't.

                    I am trying to learn this.


                    • 7. Re: CFCs &amp; CFInvoke
                      Level 7
                      <cfset myResult="foo">
                      <cfreturn MyResult>
                      you're returning a string...name you're query "MyResult", remove the
                      cfset...
                      HTH
                      --
                      Tim Carley
                      www.recfusion.com
                      info@NOSPAMINGrecfusion.com
                      • 8. Re: CFCs &amp; CFInvoke
                        Level 7
                        At no point in their examples do they set a variable for the cfreturn,
                        which is giving me an error if I don't.

                        I suspect if you look at the cfquery tag, it has ...Name="companyRec".
                        This sets the query recordset to the variable 'companyRec' that is
                        returned in the <cfreturn ...> statement.


                        Wally Kolcz wrote:
                        > I get without something.
                        > Variable MyResult is undefined.
                        >
                        > In the CF MX Bible, all they have for a component is (minus the long stuff):
                        >
                        > <cf component>
                        > <cffunction name="GetCompany" returntype="query">
                        > <cfargument..>
                        > <cfquery...>
                        > SQL
                        > </cfquery>
                        > <cfreturn companyRec>
                        > <cfcomponent>
                        >
                        > At no point in their examples do they set a variable for the cfreturn, which
                        > is giving me an error if I don't.
                        >
                        > I am trying to learn this.
                        >
                        >
                        • 9. Re: CFCs &amp; CFInvoke
                          Level 7
                          Thanks a ton Ian. That makes a lot more sense to me. I got it to work!