1

I am having a strange problem...I have set up a cfscript for use in creating a datatables JSON object, and occasionally I am receiving an unhandled exception error "the element at position X cannot be found" The X typically is one more than my array actually has, so in my case, i have 44 elements in an array, the expression error always states "position 45 cannot be found"

heres some code

/* 44 total items in this array*/
aColumns = ['nd_event_group_id','nd_event_id', 'pref_mail_name',  'request_status_code', "life_gift_pledge_ob_amt", 'activity', ... ];
/* this will return 44 */
iColumnsLen = ArrayLen(aColumns);

...

savecontent variable="rc.aaData" {
    for (i=1; i <= rc.qResult.RecordCount ; i++) {
      writeOutput('{');
      for (col=1; col <= iColumnsLen; col++) {
        // the next line is what is referenced by the expression error 
        // "The element at position 45 cannot be found"
        writeOutput('"#aColumns[col]#":#SerializeJSON(rc.qResult[aColumns[col]][i])#'); 

        writeOutput((col NEQ iColumnsLen) ? ',' : '');
      }
      writeOutput('}');
      writeOutput((i NEQ rc.qResult.RecordCount ) ? ',' : '');
    }
  };

The strange part about this issue is that I cannot recreate the error with any precision, its a hit or miss thing that occasionally happens

this script is ran by a GET via AJAX

any ideas?

Jay Rizzi
  • 4,196
  • 5
  • 42
  • 71
  • 3
    Is this code inside a shared-scope-homed CFC method, and could you be suffering from unVARed variables? – Adam Cameron Jun 24 '13 at 16:03
  • 2
    Why are you manually creating the JSON? Have you tried `serializeJSON()` on the entire query? – imthepitts Jun 24 '13 at 16:06
  • building off of Adam's comment you can use this to check for var'd variables. Unzip and run. It's super easy. http://varscoper.riaforge.org/ – Matt Busche Jun 24 '13 at 16:14
  • @MattBusche, does varscoper support `CFSCRIPT` yet? – imthepitts Jun 24 '13 at 16:24
  • @imthepitts i don't believe so, but 99% of what I deal with is tags, so I usually forget about that detail. – Matt Busche Jun 24 '13 at 17:35
  • @imthepitts I am manually creating the JSON due to the format needed , i need it in key value pairs in the JSON object rather than data array separate from the column array – Jay Rizzi Jun 24 '13 at 17:48
  • @AdamCameron, My CFC has a couple of different functions, each with an "aColumns=['one', 'two', ...];" line in it...i thought by leaving off the var on aColumns i was essentially defining it to the local function, but what youre saying is if i run one function that has that defined, the next one assumes its global? – Jay Rizzi Jun 24 '13 at 17:51
  • 1
    @JayRizzi, leaving off the var is what causes it to be global to the CFC (shared by all functions). To ensure each instance of `aColumns` is thread-safe, define it within a function block and var scope it. `function doSomething(){var aColumns=['...'];}` – imthepitts Jun 24 '13 at 18:51
  • 2
    @JayRizzi, also, concatenating JSON strings is error prone. Instead, convert your query to an array of structures, then use `serializeJSON()` to convert to a JSON string. [Here's some code you can use.](http://stackoverflow.com/questions/16724501/how-can-i-show-the-column-names-in-my-json-file-format-through-coldfusion/16724668#16724668) – imthepitts Jun 24 '13 at 18:53

1 Answers1

1

Deriving this from the comments posted, it sounds like you have variables unVARed. All function-local variables need to be declared as such, either with the VAR keyword, or by specifically scoping them in the LOCAL scope.

If you don't do this, variables are global to the CFC instance, and accordingly shared between functions. This sounds like your problem.

This is all in the docs: "CFC variables and scope".

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
  • indeed , i had multiple loops, and they were not thread safe, see http://www.daveshuck.com/2006/11/28/thread-safety-example-var-scope-your-loop-index-in-coldfusion-cfcs/ for more info – Jay Rizzi Jun 25 '13 at 19:40