Matt Wilde
Matt Wilde

Reputation: 293

Coldfusion Query broken column structure

A coldfusion query's column can be referenced like a 2D array from what I know of my past experience. Sometimes though I get this issue.

  1. create query from spreadsheet
  2. put column names into an array
  3. only get first element when trying to access the row
  4. have to use a workaround

//imports is a query object after this function
var imports = convertSpreadsheetWOHeaders(spreadsheet, 1);
//this is used to give name values in the json struct, not to access the query columns
var jsonHeaders = ListToArray("number,constructiontype,description,site_address,parcel,permit_date,note_Applicant,note_Contractor,valuation,note_Bld_Fees,note_Other_Fees");
//this gives me ["col_1","col_2","col_3",,,etc]. used to access query columns
var columnHeaders = imports.getColumnNames();
writeDump(imports[columnHeaders[1]]);
writeDump(imports);

output of the writeDump

I am left with just the first element in column one. And I get of course:

Message: You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

When trying to do this:

structInsert(jsonStruct,jsonHeaders[j],imports[columnHeaders[j]][i]);

However, this works:

 writeDump(ListToArray(ArrayToList(imports[columnHeaders[1]],'|'),'|',true));

converted to list then array

I first do a dump of imports["col_1"] and i get only the first element. Then I do a dump of ListToArray(ArrayToList(imports["col_1"])) like you see in the above image and it gives me the whole column.

Why can I not access the column correctly in the first place?

Upvotes: 2

Views: 183

Answers (1)

Matt Wilde
Matt Wilde

Reputation: 293

The Real Problem:

I was originally trying to access the jsonHeaders list as an array without doing ListToArray() on it. After that my function worked.

BUT. This next part is helpful.

When attempting to access a query object, doing queryObject["columnName"] is considered a shortcut of queryObject["columnName"][1] so coldfusion will just give you the first element.

But when I said ListToArray(ArrayToList()) coldfusion sees that ArrayToList MUST take in an array so an exception is made and the column is returned as an array.

To get a column back as an array to work with you can do a couple things

ListToArray(ArrayToList(query["column"]));
ListToArray(valueList(query.column));
valueArray(query, "column");

Upvotes: 3

Related Questions