我是 Javascript/jquery 的新手。我正在编写一个简单的 js 文件来解析 csv 文件。
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.
}
});
当我运行上面的代码时,出现以下错误:
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]
有人可以建议一下吗?谢谢!
最佳答案
你不能像在 Node.js 领域那样使用 jQuery。
您需要依赖 fs
用于读取文件的模块,以及 csv library为了实际解析您的数据,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);
不要忘记在需要之前运行npm install csv
!
关于javascript - 无法在nodejs中使用jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36273834/