I'm running this. Is there something weird with Dates and CSV, for some reason when I try to console.log my statements, Date reads as undefined and the others read fine.
Hi Willina! As-is, the dates should just be processed as strings, so if the other string is printing fine, the date should too...
Just to test, I created a new directory, did a local install of csv-parser, and here's the code I'm running (copied verbatim):
// app.jsconstcsv=require('csv-parser');constfs=require('fs');constfilepath="./example_data.csv"fs.createReadStream(filepath).on('error',()=>{// handle error}).pipe(csv()).on('data',(row)=>{letstr=`${row["BUYER NAME"]} bought ${row["CANDY PURCHASED"]} pieces of candy on ${row["PURCHASE DATE"]} and paid $${row["CASH PAID"]}.`;console.log(str)}).on('end',()=>{// handle end of CSV})// example_data.csvPURCHASEDATE,CANDYPURCHASED,CASHPAID,BUYERNAME2016-04-03,1000,10000.11,CharlieKelly2017-11-14,1000,12000.22,FrankReynolds2018-01-20,2000,40000.33,FrankReynolds2018-03-20,2000,40000.44,Mac2019-01-02,2000,50000.55,SweetDee2019-01-02,1500,13500.66,DennisReynolds// consoleIsas-MacBook-Pro:node-csv-parserisalevine$nodeapp.jsCharlieKellybought1000piecesofcandyon2016-04-03andpaid$10000.11.FrankReynoldsbought1000piecesofcandyon2017-11-14andpaid$12000.22.FrankReynoldsbought2000piecesofcandyon2018-01-20andpaid$40000.33.Macbought2000piecesofcandyon2018-03-20andpaid$40000.44.SweetDeebought2000piecesofcandyon2019-01-02andpaid$50000.55.DennisReynoldsbought1500piecesofcandyon2019-01-02andpaid$13500.66.
Any discrepancies between the code above and what you're running? Feel free to reply with the code itself and I can take a look :)
I'm sorry, I was running this with my own data, everything shows up except the date which is formatted like this: 2017:12:16 12:45. The header shows up, but when I try to access the rows, I get undefined. Could the formatting be the issue?
Hi Willina, I'm so sorry I missed your reply!! I hope you were able to get unstuck--if not, I'd be happy to take a look at the data you were using, and see if we can find what's resulting in undefined! :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'm running this. Is there something weird with Dates and CSV, for some reason when I try to console.log my statements, Date reads as undefined and the others read fine.
Hi Willina! As-is, the dates should just be processed as strings, so if the other string is printing fine, the date should too...
Just to test, I created a new directory, did a local install of
csv-parser
, and here's the code I'm running (copied verbatim):Any discrepancies between the code above and what you're running? Feel free to reply with the code itself and I can take a look :)
I'm sorry, I was running this with my own data, everything shows up except the date which is formatted like this: 2017:12:16 12:45. The header shows up, but when I try to access the rows, I get undefined. Could the formatting be the issue?
Hi Willina, I'm so sorry I missed your reply!! I hope you were able to get unstuck--if not, I'd be happy to take a look at the data you were using, and see if we can find what's resulting in
undefined
! :)