Highlighted

The value returned from the function is not of type query

New Here ,
Sep 16, 2015

Copy link to clipboard

Copied

I couldn't figure out why I got this error.  I have set default var query.  Please help.  If qLocal has records then return fine, but when qLocal records is empty will throw errors.  I've set a default.

<cffunction name="getName" access="public" returnType="query" output="false"
hint="This method returns a query.">
  <cfargument name="id_fk" required="no" default="0">
   <cfargument name="id2_fk" required="no" default="0">
 
  <cfset var qQuery = "">
   
   <cfquery name="qLocal" datasource="#db#">
         SELECT *
         FROM tbl
         WHERE id = <cfqueryPARAM value ="#id_fk#" CFSQLType ="CF_SQL_INTEGER"> AND id2 = <cfqueryPARAM value ="#id_fk2#" CFSQLType ="CF_SQL_INTEGER">
   </cfquery>


  <cfif qLocal.recordcount>
        <cfquery name="qQuery" datasource="db">                

          SELECT *

          FROM tbl2

          WHERE tbl2id = <cfqueryPARAM value ="#qLocal.id#" CFSQLType ="CF_SQL_INTEGER">

     </cfquery>

  </cfif>

  <cfreturn qQuery>
</cffunction>

The query named qQuery only executes if the qLocal query returned records. You initialized qQuery as a variable, which is not a query object. If you initialize qQuery as a query object using QueryNew(columnlist) that should work.

Views

609

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

The value returned from the function is not of type query

New Here ,
Sep 16, 2015

Copy link to clipboard

Copied

I couldn't figure out why I got this error.  I have set default var query.  Please help.  If qLocal has records then return fine, but when qLocal records is empty will throw errors.  I've set a default.

<cffunction name="getName" access="public" returnType="query" output="false"
hint="This method returns a query.">
  <cfargument name="id_fk" required="no" default="0">
   <cfargument name="id2_fk" required="no" default="0">
 
  <cfset var qQuery = "">
   
   <cfquery name="qLocal" datasource="#db#">
         SELECT *
         FROM tbl
         WHERE id = <cfqueryPARAM value ="#id_fk#" CFSQLType ="CF_SQL_INTEGER"> AND id2 = <cfqueryPARAM value ="#id_fk2#" CFSQLType ="CF_SQL_INTEGER">
   </cfquery>


  <cfif qLocal.recordcount>
        <cfquery name="qQuery" datasource="db">                

          SELECT *

          FROM tbl2

          WHERE tbl2id = <cfqueryPARAM value ="#qLocal.id#" CFSQLType ="CF_SQL_INTEGER">

     </cfquery>

  </cfif>

  <cfreturn qQuery>
</cffunction>

The query named qQuery only executes if the qLocal query returned records. You initialized qQuery as a variable, which is not a query object. If you initialize qQuery as a query object using QueryNew(columnlist) that should work.

Views

610

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 16, 2015 0
Advocate ,
Sep 16, 2015

Copy link to clipboard

Copied

qQuery vs. qLocal.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 16, 2015 0
New Here ,
Sep 16, 2015

Copy link to clipboard

Copied

I think I understand but can you clarify?  Or maybe I have to create 2 functions for this?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 16, 2015 0
Participant ,
Sep 16, 2015

Copy link to clipboard

Copied

The query named qQuery only executes if the qLocal query returned records. You initialized qQuery as a variable, which is not a query object. If you initialize qQuery as a query object using QueryNew(columnlist) that should work.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 16, 2015 0
Advocate ,
Sep 16, 2015

Copy link to clipboard

Copied

I didn't notice the conditional query and what mkanel states is correct - qQuery will be defined if results are returned in your qLocal query but will be undefined (really an integer with a value of 0) if no results are found. There is a possible second problem here in that you never declare qLocal. If it really is a "local" it needs to be declared like qQuery, otherwise it will default to the variables scope -- which is not local. For your simple test, it'll probably work fine but on a busy site you'll run into cross-threading issues that'll probably return some strange results that'll be hard to debug. I tend to not use the var declaration and instead scope all my variables using local for local: local.qQuery and local.qLocal. This way I can look at the code and visually see the scope as opposed to assuming it is declared correctly.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 16, 2015 0
New Here ,
Sep 16, 2015

Copy link to clipboard

Copied

Thanks for the explanations.  It helps me a lot of these scope.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 16, 2015 0