-1

I came across an error today, and I am not sure why it occurred. At the second for loop, the access to data will be undefined instead of the data that was inserted as a parameter into the function. Somehow the data gets the value of null, instead of the Object that was passed to this function. Does anyone know why? The error I get is "data is not defined".

     createDataObject: function (data) {
        let dataObj = data.source[0];
        let backedData = data.backedData;
        for (let i in dataObj) {
            let d = dataObj[i];
            d.data = { source: d.data };
        }
        for (let i = 0; i < data.backedData.length; i++) {
            let bd = data.backedData[i];      //<- this is where the error occurrs
            let data = bd.data[0];
        }
}

Here is some code outside of the object, if you want to try, that I was using, this will work on your console or in node. Seems like I have come across a Javascript compiler bug. I am not sure.

createDataObject({source: [[{data: 1}]],  backedData: [1,2,3, 4]});

The code above will work if I do the following

    createDataObject: function (data) {
        let dataObj = data.source[0];
        let backedData = data.backedData;   //create the backedData variable here
        for (let i in dataObj) {
            let d = dataObj[i];
            d.data = { source: d.data };
        }
        for (let i = 0; i < backedData.length; i++) {
            let bd = backedData[i]; // no Error
            let data = bd.data[0];
            //.....
        }
    }
Jackstine
  • 486
  • 4
  • 12
  • 1
    `console.log( backedData )` – Jonas Wilms Dec 06 '17 at 18:03
  • 2
    `null` is a very different thing from "not defined". What is the *exact* error? Is it truly that *exact* line which throws it? What happens when you log the relevant variables to the console? – David Dec 06 '17 at 18:04
  • backData is not undefined, data is. – Jackstine Dec 06 '17 at 18:04
  • 2
    @Jackstine But then it would have thrown on the first line already – Bergi Dec 06 '17 at 18:04
  • 1
    There is nothing special about the for loop. You can make this the first line of code in the function: `console.log(data.backedData[0])`. It expects backedData to be an array. – agm1984 Dec 06 '17 at 18:07
  • 2
    Unable to replicate the behavior with the code provided: https://jsfiddle.net/qx1dm2rs/1/ – David Dec 06 '17 at 18:08
  • At the beginning of the first instance of `createDataSource` you should insert `console.log(data);` – Gershom Maes Dec 06 '17 at 18:11
  • Ok so I deleted some code from the first edit. It looks like I created a new data variable inside the for loop, this caused the issue, before data was not created in the for loop yet. My bad. Thanks for the help everyone. – Jackstine Dec 06 '17 at 18:17
  • That makes all the difference. It's called a "temporal dead zone". https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let Verifying the example code you provide is important, and should be done before you post. –  Dec 06 '17 at 18:20
  • Possible duplicate of [Are variables declared with let or const not hoisted in ES6?](https://stackoverflow.com/questions/31219420/are-variables-declared-with-let-or-const-not-hoisted-in-es6) –  Dec 06 '17 at 18:26

1 Answers1

0

in the second for loop you create another data object. Data is not created on the first line in the for loop. Try using

   for (let i = 0; i < data.backedData.length; i++) {
        let bd = data.backedData[i];
        let d= bd.data[0];   // Convert data to d or another variable name
        //.....
    }
Jackstine
  • 486
  • 4
  • 12