Javascript 为什么我会得到;TypeError:对象不是函数;运行这个示例node.js代码?
为什么运行下面显示的代码时会出现错误?(这是在Coursera上斯坦福大学“创业工程”课程的一个家庭作业中使用的:) 该课程于2013年6月至9月开课,所以可能是node或csv的更新破坏了脚本?作业不是关于修改脚本,所以这个问题不是“作弊”,而且课程目前也没有运行 因此,环境是: Ubuntu 14.04(内核3-13-0-29-generic), 节点v0.11.13, npm v1.4.9 我在主目录中安装了Javascript 为什么我会得到;TypeError:对象不是函数;运行这个示例node.js代码?,javascript,node.js,csv,Javascript,Node.js,Csv,为什么运行下面显示的代码时会出现错误?(这是在Coursera上斯坦福大学“创业工程”课程的一个家庭作业中使用的:) 该课程于2013年6月至9月开课,所以可能是node或csv的更新破坏了脚本?作业不是关于修改脚本,所以这个问题不是“作弊”,而且课程目前也没有运行 因此,环境是: Ubuntu 14.04(内核3-13-0-29-generic), 节点v0.11.13, npm v1.4.9 我在主目录中安装了npm'd csv、accounting util和restler,脚本也在那里
npm
'd csv、accounting util和restler,脚本也在那里
这让我完全不知所措-(
错误消息:
[ubuntu@ip-xxx-xxx-xxx-xxx:~]$node market-research.js
Invoked at command line.
Wrote market-research.csv
Symbol Name Market Cap Previous Close Price P/E Ratio Shares EPS Earnings
/home/ubuntu/market-research.js:47
csv()
^
TypeError: object is not a function
at csv2console (/home/ubuntu/market-research.js:47:5)
at Request.response2console (/home/ubuntu/market-research.js:65:13)
at Request.EventEmitter.emit (events.js:110:17)
at Request.mixin._fireSuccess (/home/ubuntu/node_modules/restler/lib/restler.js:226:10)
at /home/ubuntu/node_modules/restler/lib/restler.js:158:20
at IncomingMessage.parsers.auto (/home/ubuntu/node_modules/restler/lib/restler.js:394:7)
at Request.mixin._encode (/home/ubuntu/node_modules/restler/lib/restler.js:195:29)
at /home/ubuntu/node_modules/restler/lib/restler.js:154:16
at Request.mixin._decode (/home/ubuntu/node_modules/restler/lib/restler.js:170:7)
at IncomingMessage.<anonymous> (/home/ubuntu/node_modules/restler/lib/restler.js:147:14)
[ubuntu@ip-xxx-xxx-xxx-xxx:~]$
脚本创建的market-research.csv的内容:
"GOOG","Google Inc.",386.6B,569.20,29.49
"FB","Facebook, Inc.",194.5B,73.855,78.49
"AAPL","Apple Inc.",613.8B,102.25,16.49
"YHOO","Yahoo! Inc.",38.302B,38.31,33.11
"MSFT","Microsoft Corpora",374.3B,44.88,17.06
"LNKD","LinkedIn Corporat",27.747B,223.26,N/A
"CRM","Salesforce.com In",36.577B,58.29,N/A
Quantas 94重,谢谢你的提示 12个月前,节点csv的版本是0.2,但现在是0.4,API也发生了变化 我现在编写了csv控制台函数,如下所示:
// part of file market-script.js
// edited by Jeff Moye (Moyesoft)
// no attribution in the original file
var csv2console = function(csvfile, headers) {
console.log(headers.join("\t"));
var parser = csv.parse(); //create the parser
parser.on('readable', function(){ //the 'record' event no longer exists
// so instead we write a handler for the
// 'readable' event
while(record = parser.read()){ // and read() each record in a loop
var shares = Math.round(marketCapFloat(record[2])/record[3], 0);
var eps = (record[3]/record[4]).toFixed(3);
var earnings = accounting.formatMoney(eps * shares);
outrow = record.concat([shares, eps, earnings]);
console.log(outrow.join("\t"));
}
});
parser.on('error', function(err){ // we may as well have an error handler
console.log(err.message);
});
fs.createReadStream(csvfile).pipe(parser); //open file as a stream and pipe it to the parser
};
模块csv是否正确安装?您使用的是什么版本以及在何处使用?在使用之前是否尝试记录csv变量?是否确定使用的是正确的版本?当前版本的API似乎与您的代码不同。我认为csv不是一个函数。csv是一个对象及其公开函数csv.generaste,csv.parse,csv.transform,csv.stringify;因此只能使用这些函数中的任何一个…谢谢!我自己永远不会想到这一点。我正在编写结束后的问题如何解决它?
// part of file market-script.js
// edited by Jeff Moye (Moyesoft)
// no attribution in the original file
var csv2console = function(csvfile, headers) {
console.log(headers.join("\t"));
var parser = csv.parse(); //create the parser
parser.on('readable', function(){ //the 'record' event no longer exists
// so instead we write a handler for the
// 'readable' event
while(record = parser.read()){ // and read() each record in a loop
var shares = Math.round(marketCapFloat(record[2])/record[3], 0);
var eps = (record[3]/record[4]).toFixed(3);
var earnings = accounting.formatMoney(eps * shares);
outrow = record.concat([shares, eps, earnings]);
console.log(outrow.join("\t"));
}
});
parser.on('error', function(err){ // we may as well have an error handler
console.log(err.message);
});
fs.createReadStream(csvfile).pipe(parser); //open file as a stream and pipe it to the parser
};