Search code examples
meteorpapaparse

Papaparse looping through rows: unexpected token error


So I'm trying to parse a csv file using papaparse in Meteor, the following is the code:

var csv = Assets.getText('test.csv');

Papa.parse(csv, {
header:true,
complete: function(results) {
    results.data.forEach(row){

    }
    console.log(results);
}
});

It's giving me an unexpected token, expected ";" error on the results.data.forEach(row){ line. If I put for example var testword = 'x'; inside the brackets I get the same error. I was trying to loop through each row but I'm not sure why It's not letting me do so. Any ideas?


Solution

  • In situations like this it's always useful to do a search for the documentation of the function you are using. In this case the forEach function. Docs are here

    Your mistake is that you are not passing a callback function to forEach as the first argument.

    Modify your code to the following:

    results.data.forEach(function(row) {
        // now you can loop through each row in here
    });
    

    As pointed out by MasterAM above, if you are using ES6 you can also use an arrow function to make this shorter:

    results.data.forEach((row) => {
        // loop through each row here
    });