Search code examples
javascriptnode.jscsvpapaparse

Issues with exporting PapaParse function


I am new to JavaScripting and Node and am putting a proof of concept together for my first application. My goal is to:

  • Parse a CSV file (since the data I want is way to small to warrant a database)
  • Load the parsed data into an Array
  • Search the Array for a specific 'Date' which will have two other fields I can pull data from
  • Present that data as an exported function (please forgive my phrasing if inaccurate)

With help from a previous co-worker, I found PapaParse and lodash to help with parsing the data and searching it. The application works great if I call the function in the same application. When I try to export the function I get no results. I have been trying to solve this problem on my own for two weeks now and am hopeful someone can help me.

filename: newFOTD.js

var papa = require('papaparse');
var _ = require('lodash');
var fs = require('fs');
var csvfile = '../data/flavorDB.csv';

function flavorOfTheDay(date) {
    papa.parse(fs.createReadStream(csvfile), {
        header: true,
        delimiter: ",",
        complete: function(results) {
            var match = _.filter(results.data, _.matches({'Date': (date)}));
            match.forEach(function (flavorDB) {
                if (flavorDB.Note.length != "") { /* eslint-disable no-console */
                    console.log("Today's flavor is " + flavorDB.Flavor + ". Did you know that today is also " + flavorDB.Note + "? How cool!");
                } else console.log(flavorDB.Flavor);
            })
        }
    })
}
flavorOfTheDay('2018-08-09');

module.exports.flavorOfTheDay = flavorOfTheDay

The above works great. When I try to access the exported function, I get no data back.

filename: program.js

var test = require('./lib/newFOTD');

test.flavorOfTheDay('07-08-2018')

I must be doing something wrong with Papaparse and cannot figure out what it is. I can place a simple console.log(date) inside of the flavorOfTheDay function outside of the Papaparse logic and when I call the function from 'program.js' I will get the date data that I pass back in the console. I would greatly appreciate any help or pointing me into the right direction. What I thought was going to be a simple test to allow me to move on to the next phase of my proof of concept has turned into a loss of sleep and frustrating couple of weeks, LOL. Thank you.


Solution

  • The issue with PapaParse was how I was declaring the variable for the CSV file for parsing and needing to use path to make it work.

    CORRECT WAY

    filename: newFOTD.js
    
    var path = require('path');
    var csvfile = path.join(__dirname, '..', 'data', 'flavorDB.csv');
    

    INCORRECT/ORIGINAL WAY

    filename: newFOTD.js
    
    var csvfile = '../data/flavorDB.csv';
    

    Thank you to David Boskovic for helping answer, what I thought was an issue with the PapaParse code. Once I thought it was an issue with code I opened an issue on Github. Calling Papaparse as an exported module not returning data.