1

I have made two functions to parse the CSV data with Papaparse, and one function to get the value from that data. The two functions use a return statement.

The problem I have is that the data I receive in Papaparse is always undefined. The value I would like to get is a mean value of the data in the CSV file. Here is a code snippet of where I would like the mean to be received:

function parseData() {

    var csvfile = "probeersel11.csv";

    $.get(csvfile, function (data) {
        var csvdata = Papa.parse(data, {
            header: true,
            skipEmptyLines: true,
            dynamicTyping: true
        });
        alert(doStuff(csvdata));

    });
}

The question is: how do I receive the right value here?

tehbobshow
  • 47
  • 1
  • 6
  • I tried your code and it works. Changed nothing, except for commenting the `doStuff()` call. `csvdata` is not empty despite Sugar's answer — I can see the parsed object. Using Papaparse v4.1.2. – Matvey Andreyev Feb 09 '17 at 13:23
  • @MatveyAndreyev not sure it is parsed object. I think it's parser instance – Sugar Feb 10 '17 at 12:28

1 Answers1

1

according to docs

Papa.parse(file, config)

file is a File object obtained from the DOM.

config is a config object which contains a callback.

Doesn't return anything. Results are provided asynchronously to a callback function.

so csvdata will be empty. You should specify callback in your config and only then pass somewhere else

Community
  • 1
  • 1
Sugar
  • 490
  • 9
  • 22