Diego Vinícius
Diego Vinícius

Reputation: 2223

queryEach or .each() not Working at CF 11, Why?

I was seaching in google better form to interate over "query" in coldfusion, since im new in the company im working, and im trying to get more from CF

Here my attempts:

My models:

<cffunction hint="Foo" name="Foo" access="public" returntype="query">
    <!--- Argumentos --->
    <cfargument hint="Something" name="ArgComBus" type="string" required="no" default="">
    <cfargument hint="Other thing" name="ArgPar" type="string" required="no" default="">

    <cfscript>
        queryService = new Query();
        queryService.setSql("
            SELECT
                column1,
                column2,
            FROM
                tab_bar
            WHERE
                1=1
                #arguments.ArgComBus#
        ");
        queryService.setDataSource(session.Dsn);

        if(Len(Trim(arguments.ArgPar))){
            Evaluate(arguments.ArgPar);
        }
        queryResult = queryService.execute();
        qBus = queryResult.getResult();
    </cfscript>


    <cfreturn qBus>
</cffunction>

My script

<cfscript>
arrFoo = arrayNew(1);

qFoo = this.Foo(
    ArgComBus = " AND column1 = #variables.bar# ");

// First Attempt - The each method was not found.
qFoo.each(function (foo) {
    arrFoo.append(foo);
});

// Second Attempt - Variable QUERYEACH is undefined.
queryEach(qFoo, function (foo) {
    arrFoo.append(foo);
});

writeDump(arrFoo);
</cfscript>

My Server Dump

InstallKit  Native Windows
appserver   Tomcat
productlevel    Developer
productname ColdFusion Server
productversion  11,0,05,293506
rootdir C:\CFusion11\cfusion

I even used getMetaData() on my query variable qFoo and that return that is array... so when i tried use something like that (trying to convert array in query?)

cfQuery = createObject("java", "coldfusion.sql.QueryTable").init(qFoo); 

.each() and queryEach() same answer... i even tried use arrayEach() but return the object is coldfusion.sql.QueryTable and not array

Upvotes: 1

Views: 249

Answers (1)

Miguel-F
Miguel-F

Reputation: 13548

You are running ColdFusion 11.

The queryEach() function was not added until ColdFusion 2016:

Originally I had posted that the each() member function was not available in Adobe ColdFusion 11. Aquitaine pointed out in the comments that it actually is. I incorrectly referenced the Each() function for Lucee that works with collections. The Each() function related to this question is actually the script version of the ArrayEach() tag function. Which is available in ColdFusion 11 (it was actually added in ColdFusion 10). Sorry for the confusion.

The documentation may be wrong. I could not get the function to work as Each() except under ColdFusion 2018. For ColdFusion 11 I could only get it to work as ArrayEach().

Here are a couple of examples on how to loop over a query in ColdFusion 11 (borrowed from cfdocs):

// Define our query 
platform = ["Adobe ColdFusion", "Railo", "Lucee"]; 
myQuery = queryNew(" "); 
queryAddColumn(myQuery, "platform", "CF_SQL_VARCHAR", platform); 


// By row index 
for (i = 1; i <= myQuery.recordCount; i++) { 
   writeOutput("<li>#myQuery["platform"][i]#</li>"); 
} 


// By query 
for (row in myQuery) { 
   writeOutput("<li>#row.platform#</li>"); 
}


// By arrayeach 
writeOutput("<h3>By arrayeach:</h3>");
function printArray(vendor, index)
{
    writeOutput("<li>#vendor#</li>");
}
arrayEach(platform,printArray);

I created a gist for you on TryCF.com so you can see this code in action and play around with it if you like. Just click here to run the code.

Upvotes: 6

Related Questions