using a query as an argument to a cffunction

1.9k Views Asked by At

I would like to pass a query as a parameter to a function.

According to the docs You can specify query as an argument type.

However, when I try doing this:

<cfquery name="share_types_query" datasource="phonebook">       
    SELECT share_loan_type, share_loan_description
    FROM shareloantypes
    WHERE share_loan='S'
    ORDER BY  share_loan_type
</cfquery>
<cfscript>
    phonebookQuery(share_types_query, "SHARE_TYPES");
</cfscript> 


<!--function to take a query and turn the result into a JSON string-->
<cffunction name="phonebookQuery" access="public" returnType="void">
    <cfargument name="query" type=query required="yes">
    <cfargument name="jsVar" type=string required="yes">

    <cfset json = "[ "/>
    <cfloop query="query">
        <cfset json = json & "{ "/>
        <cfset i=1/>
        <cfloop list="#arrayToList(query.getColumnList())#" index="col">
            <cfset json = json & '"#col#"' & ": "/>
            <cfset json = json & '"' & query[col][currentRow] & '"'/>
            <cfif i lt ListLen(query.columnList)>
                <cfset json = json & ", "/>
            </cfif>
            <cfset i= i+1/>
        </cfloop>
        <cfset json = json & " }"/>
        <cfif currentRow lt recordCount>
            <cfset json = json &","/>
        </cfif>
    </cfloop>
    <cfset json = json & " ]"/>

    <script type="text/javascript">
        <cfoutput>
            var #toScript(json,Arguments.jsVar)#;
        </cfoutput>
    </script>
</cffunction>

I get the following error:

The parameter SELECT to function phonebookQuery is required but was not passed in.

I am very new to CF and I am on CF MX 7.

1

There are 1 best solutions below

1
On BEST ANSWER

The version of the question that I posted wasn't actually a carbon copy of the function in the source file. The function in the source actually had some <cfargument> tags (the first one being name="select") that were commented out.

I was commenting them like this: <!-- a comment --> which triggered the appropriate response in my syntax highlighter, but ColdFusion was still trying to execute the lines.