0

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.

Jeremy M
  • 179
  • 2
  • 13
  • you have to pass `callback` to `flavorOfTheDay` in `program.js` – Mukesh Sharma Jul 30 '18 at 13:30
  • @MukeshSharma, How would I go about that? I tried that at one point. When I pass callback `test.flavorOfTheDay('07-08-2018', callback)` in `program.js` I get a response saying **ReferenceError: callback is not defined**. – Jeremy M Jul 30 '18 at 16:59
  • @MukeshSharma, I attempted this update to `program.js` with no luck. I do not get an error, just get no data. `test.flavorOfTheDay(function(date, data) { console.log(data)})` That is one method I found. I also tried `test.flavorOfTheDay('07-08-2018', function(err, result) { console.log(result)})` with the same result. No error, just no data returned. – Jeremy M Jul 31 '18 at 12:25

2 Answers2

0

I think what @MukeshSharma is trying to say is: Since your function is asynchronous, you have to provide a callback-function when calling the flavorOfTheDay()-function. Just like you did in the first code snippet: flavorOfTheDay('07-12-2018', doStuff) --> the doStuff is your callback function.

Modified example from above as anonymous callback function:

filename: program.js

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

test.flavorOfTheDay('07-08-2018', function(date, data) {
  // do whatever you like with your data here
  console.log(date);
  console.log(data);
})
  • First, thank you for posting, second, my apologies for taking so long to respond, my daughter had surgery yesterday. I think there is a bug in Papa Parse or it wasn't designed to work this way. After my initial failure, I spent a lot of time reading up on asynchronous functions, callbacks and how to provide a callback function when calling a function from another app because I felt pretty stupid. I get no results when I rune `node program.js` even with the callback function defined. When I switch from _papaparse_ to _csv-parse_, it works great. – Jeremy M Aug 02 '18 at 18:19
0

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.

Jeremy M
  • 179
  • 2
  • 13