0

I'm trying to load two user-defined csv files and programmatically compare them (essentially trying to find errors in the files and report those errors to the user). I'm uploading the first and am running into a problem. When I display the entire array console.log(results); it shows me the entire array in the console. However if I try to get the first row console.log(results[0]); I simply see 'undefined' in the console. I also have the same issue when I call console.log(deviceData); in a later snippet of code (works for the entire array but the second I try to access an element I get an undefined). What am I doing wrong?

<script>
    var deviceData = new Array();
    const uploadConfirm = document.getElementById('uploadConfirm').addEventListener('click', () => {
        Papa.parse(document.getElementById('deviceFile').files[0],
        {
            download: true,
            header: false,
            skipEmptyLines: true,
            complete: function(results){
                console.log(results[0]);
                deviceData = results;
            }
        });
    });
Rusty
  • 1
  • 1
  • You don't have `console.log(results);` anywhere in your code. Was it in the exact same spot as where `console.log(results[0]);` is? – John Montgomery Feb 07 '22 at 22:55
  • 2
    What is `Papa` ? This can be caused by how the browser's console works. It can be that when `console.log(results[0])` is executed, `results` is empty, and then, when it is populated, the browser's console updates and it shows its latest content. – Titus Feb 07 '22 at 22:58
  • I don't understand what API `Papa.parse` refers to, BUT you should test to see if the results response is parsed or what type of format it is. Try `console.log(typeof results)` – Kinglish Feb 07 '22 at 23:00
  • My apologies, yes, `console.log(results[0])` is where I am adding and removing the `[0]`. `console.log(results)` works, `console.log(results[0])` does not. I am using Papa Parse. I reference it in my code earlier on with this: `` – Rusty Feb 07 '22 at 23:10
  • `console.log(typeof results)` shows `object` in the console – Rusty Feb 07 '22 at 23:11
  • The console updates references live, what you are seeing for `console.log(results)` is the updated value once your parser completes its **asynchronous** action. See [this post](https://stackoverflow.com/q/7389069/283366) for more on why the console is **not** a good debugging tool – Phil Feb 07 '22 at 23:12
  • Based on the [documentation](https://www.papaparse.com/docs#results) of the library that you're using, the actual data is under `results.data`, try `console.log(results.data[0])`, that should give you the expected output. – Titus Feb 07 '22 at 23:14
  • Sure enough, `console.log(results.data[0])` worked for that part of the code, thanks Titus. For the second part, trying to assign an object to an array was the issue (thank you for pointing me in the right direction Kinglish). I changed the assignment to `deviceData = Object.values(results);` and now I can access `deviceData` properly. – Rusty Feb 07 '22 at 23:26
  • 1
    It looks like my question shows "[duplicate]" and includes a link to a question claimed to be related, however I wanted to point out the linked question did not solve my problems. My problems were solved by accessing the correct place for the data and assigning the right type of data (see above comment). – Rusty Feb 08 '22 at 17:37

0 Answers0