0

I am newbie to Javascript/jquery. I am writing a simple js file that parses a csv file.

var jquery = require('jquery');

jquery.get('file.csv', function(data) {
   alert(data); // this is a line
   var tempArray = data.split(','); // array of data
   for(var i = 0; i < tempArray.length; i++)
   {
       console.log(tempArray[i]); // probably index 1 is your IPv6 address.
   }
});

When I run the code above, I get the following error:

jquery.get('file.csv', function(data) {
       ^

TypeError: jquery.get is not a function
    at Object.<anonymous> (/Users/ishabir1/Desktop/transloc/parser.js:3:8)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:442:10)
    at startup (node.js:136:18)
    at node.js:966:3
[Finished in 0.1s with exit code 1]

Can someone please advise? Thanks!

2
  • Dude, try not to use client side libraries on server-side. They will mess things up. Node has various modules to play with file system and parsing data, where fs in a core module. Commented Mar 29, 2016 at 1:39
  • @Nivesh you're right. I was confusing the client vs server side frameworks. Thanks! Commented Mar 29, 2016 at 1:41

1 Answer 1

5

You cannot use jQuery like that in the Node.js realm.

You need to rely on the fs module for reading files, and on a csv library for actually parsing your data, see example:

var fs = require('fs');
var parse = require('csv').parse;

var parser = parse(function(err, data){
  console.log(data);
});

fs.createReadStream('file.csv').pipe(parser);

Don't forget to run npm install csv before requiring it!

Sign up to request clarification or add additional context in comments.

5 Comments

what does this line do: var parse = require('..');
I npm installed csv, and now I get the error that parse is not a function.
See edit again, I took the example from the csv-parsesubmodule.
Thank you, it does work now! Is this the best way to parse a csv file in a nodejs application?
If you want to be a Jedi in csv parsing kindly refer to node-csv and there Github repo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.