1

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.

Luke
  • 5,567
  • 4
  • 37
  • 66
  • 4
    It looks like CF is calling another function, not the function you posted since the function you post does not have ``. This could be because of caching and previously `phonebookQuery()` has a `SELECT` param? – Henry May 19 '15 at 21:25
  • 2
    Off-topic a little, but if you can get up to version 9+ or on Lucee you'd be in a much better position. You could then let ColdFusion handle building your JSON for you. I know you may not have that option though. – Chris Tierney May 19 '15 at 21:36
  • 1
    I agree with @Henry. The code you posted is not the code that's erroring. – Adam Cameron May 19 '15 at 22:15
  • @Henry actually there was an old version of the function that took a parameter named select. Perhaps that is the case. Ill have to wait until I get into work tomorrow to find out. – Luke May 19 '15 at 23:32
  • OT: the cfset tag does not need to be closed. `` looks like a typo – James A Mohler May 19 '15 at 23:48
  • @LukeP http://www.carehart.org/blog/client/index.cfm/2012/12/13/clearing_the_template_cache_programmatically – Henry May 20 '15 at 00:58
  • OT: inside CF functions you need to scope variables. You can either do this using `var xyz` or via the inbuilt `local.xyz` scope otherwise you could end up with some hard to track down bugs in production. – John Whish May 20 '15 at 08:00
  • Not an answer to your question, but ... do not roll your own JSON :) Instead [use one of older functions/cfc's](http://stackoverflow.com/questions/25190654/how-to-parse-json-returned-in-coldfusion/25252656#25252656) that support MX7. – Leigh May 21 '15 at 00:32
  • @Leigh Thanks for the tip, but whats wrong with rolling my own JSON? Its already written and it works. – Luke May 21 '15 at 13:24
  • @LukeP - It may work for simple cases, but it does not handle special/reserved JSON characters. That is why is better to use an established library, rather than reinventing the wheel, because a good library will have already handled it for you. Established libraries are also better tested than home spun code and often handle issues you may not even be aware of .. yet :) – Leigh May 21 '15 at 16:49

1 Answers1

0

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.

Luke
  • 5,567
  • 4
  • 37
  • 66
  • (Edit) *ColdFusion was still trying to execute the lines* Yep. The [two dashes make it an HTML comment, rather than a CF comment (not executed)](http://stackoverflow.com/questions/10940195/difference-between-comments-with-2-dashes-vs-3-in-coldfusion/10940238#10940238).. Glad you figured it out, but for next time .. please post the actual code throwing the error :) – Leigh May 21 '15 at 16:57