Search code examples
javascriptcsvreturn-valueglobalpapaparse

Papaparse return value from csv file


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?


Solution

  • 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