lucee 5.x ListEach doesn't seem to be able to access local var scope?

Viewed 83

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

1 Answers

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.

Related