4

I can't seem to access the local scope within my ListEach:

writeDump(local.woCoreID); // outputs expected values
//  LOOP OVER LIST AND SEPARATE TEXT FROM INTEGERS
ListEach(local.__userSuppliedWorkoutTagList, function (item) {
    writeDump(item) //  outputs expected values
    writeDump(local.woCoreID); // key [woCoreID] doesn't exist
});

when I try to access the local.woCoreID, I get an error message, key [woCoreID] doesn't exist. Why is that when I can dump it before the ListEach and I see the value is there. What am I missing here?

I'm using Lucee 5.x

rrk
  • 15,677
  • 4
  • 29
  • 45
HPWD
  • 2,232
  • 4
  • 31
  • 61

1 Answers1

7

Each function has its own local scope. If you want to the outer scope, you must make a reference to it:

var outerLocal = local;

ListEach(local.__userSuppliedWorkoutTagList, function (item) {
    writeDump(item);
    writeDump(outerLocal.woCoreID);
});

or use a regular, counted for loop instead of ListEach() + function.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 1
    This. But I'd also caution that this also makes the anonymous function inside the `ListEach()` dependent on an external scope ( that can easily be changed outside the function and give an unexpected output ). Personally, I try to keep my functions as self-contained as I can make them. If I need it inside my function, then I explicitly send it there. – Shawn Sep 19 '18 at 16:46
  • I went ahead and used the tried and true `FOR` loop and it resolved the issue. I wasn't aware of the ListEach until recently so I thought I'd try it. Clearly I need to read up on it more. – HPWD Sep 19 '18 at 21:35