-1

I am having a really strange problem. I am trying to perform a for loop on a promise return value. When I run the code from a jasmine test it breaks. When I run it from a browser it breaks. The code is....

courseService.getCourseDates(8).then(function (data) {

            console.log(data[0]);  --this works
            console.log(data[1]);  --this works

            for (s in data) {
                console.log(data[s]);
            }
}

Expected output:

'2014-06-14T00:00:00'
'2014-06-14T00:00:00'
'2014-06-14T00:00:00'
'2014-06-14T00:00:00'

Actual output from Karma/Jasmine:

ReferenceError: Strict mode forbids implicit creation of global property 's'

Actual Output from browser:

ReferenceError: s is not defined

Anyone know what is wrong? I have used for loops like this in the past and clearly the array is being populated okay....

Exitos
  • 29,230
  • 38
  • 123
  • 178

1 Answers1

2

Try this:

for (var s in data) {
    ...
}
Kyle Paulsen
  • 956
  • 1
  • 8
  • 22
  • oh that's funny I have used it without the var s before in other code :-/ I will accept the answer :-) – Exitos May 08 '14 at 17:59
  • I suspect that when you’ve used it without ever declaring the variable with `var`, you were not using strict mode. – Frungi Jul 28 '14 at 17:31