无法在nodejs中使用jquery

rob*_*d91 0 javascript jquery node.js

我是Javascript/jquery的新手.我正在编写一个解析csv文件的简单js文件.

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.
   }
});
Run Code Online (Sandbox Code Playgroud)

当我运行上面的代码时,我收到以下错误:

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]
Run Code Online (Sandbox Code Playgroud)

有人可以建议吗?谢谢!

moo*_*e99 5

你不能在Node.js领域中使用类似的jQuery.

您需要依赖fs模块来读取文件,并在csv库上实际解析数据,请参阅示例:

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

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

fs.createReadStream('file.csv').pipe(parser);
Run Code Online (Sandbox Code Playgroud)

npm install csv在要求之前别忘了跑!