简体   繁体   English

导出PapaParse函数的问题

[英]Issues with exporting PapaParse function

I am new to JavaScripting and Node and am putting a proof of concept together for my first application. 我是JavaScript和Node的新手,正在为我的第一个应用程序整合概念验证。 My goal is to: 我的目标是:

  • Parse a CSV file (since the data I want is way to small to warrant a database) 解析CSV文件(因为我想要的数据很小以保证数据库的安全)
  • 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. 在以前的同事的帮助下,我找到了PapaParselodash来帮助解析数据和搜索数据。 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. 我可以在Papaparse逻辑之外的flavorOfTheDay函数中放置一个简单的console.log(date) ,当我从'program.js'调用该函数时,我将获得在控制台中返回的date数据。 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. 谢谢。

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. 我认为@MukeshSharma想要说的是:由于您的函数是异步的,因此在调用flavorOfTheDay()函数时必须提供一个回调函数。 Just like you did in the first code snippet: flavorOfTheDay('07-12-2018', doStuff) --> the doStuff is your callback function. 就像您在第一个代码段中所做的一样: flavorOfTheDay('07-12-2018', doStuff) -> doStuff是您的回调函数。

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);
})

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. PapaParse的问题是我如何声明CSV文件的变量以进行解析,并且需要使用path使其正常工作。

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. 感谢David Boskovic的帮助回答,我认为这是PapaParse代码存在的问题。 Once I thought it was an issue with code I opened an issue on Github. 一旦我认为这是代码问题,就在Github上打开了一个问题。 Calling Papaparse as an exported module not returning data . 调用Papaparse作为导出模块,不返回数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM