Node.js TypeError: path must be a string or Buffer
我正在编写一个命令行程序,它使用csv文件中的信息计算订单的总价格。
sample.catalog.csv中的数据:
1 2 3 4 5 | P1,5,1000.00 P2,8,250.00 P3,15,125.00 P4,10,250.00 P5,2,2500.00 |
程序必须从命令行运行,参数如下:
示例:$calculateOrder sample.catalog.csv p1 2 p24
(P4 6 P10 5 P12 1是csv文件中提供的产品和数量)
总数:4151,25
这就是我现在拥有的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | var program = require('commander'); const csv = require('csv'); const fs = require('fs'); program .version('1.0.0') .option('-l, --list [list]', 'list of order prices in sample.catalog.csv') .parse(process.argv) console.log("hello world") console.log("list of order prices", program.list); /* To read csv file and print the data to the console: [node orderPrice --list input/sample.catalog.csv] */ let parse = csv.parse; let stream = fs.createReadStream(program.list) .pipe(parse({ delimiter: ',' })); var total = 0; const vat = 23; const totalWithVat = total * vat; stream .on('data', function (data) { let product = data[0]; let quantity = data[1]; let price = data[2]; console.log(product, quantity, price); calculateOrder = () => { if (quantity > 20) { stream.destroy(new Error("Quantity exceeds stored amounts")); } total += price * quantity; } }) .on("finish", function () { console.log("Total price:", totalWithVat); }) .on("error", function (error) { console.error("The following error occured:", error); }) |
我有以下错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | λ node orderPrice calculateOrder sample.catalog.csv P1 2 P2 4 hello world list of order prices undefined fs.js:636 binding.open(pathModule._makeLong(path), ^ TypeError: path must be a string or Buffer at Object.fs.open (fs.js:636:11) at ReadStream.open (fs.js:1982:6) at new ReadStream (fs.js:1969:10) at Object.fs.createReadStream (fs.js:1923:10) at Object. (E:\order-price\orderPrice.js:31:17) at Module._compile (module.js:643:30) at Object.Module._extensions..js (module.js:654:10) at Module.load (module.js:556:32) at tryModuleLoad (module.js:499:12) at Function.Module._load (module.js:491:3) |
我是node.js的新手,非常感谢您的帮助。谢谢您。
换行
1 | let stream = fs.createReadStream(program.list) |
到
1 | let stream = fs.createReadStream(program.argv[some number]) |
其中某个数字是您提到文件名的位置
例如,使用以下命令运行程序
1 | node test.js somevar filename |
那么somenumber=3
1 2 3 4 | 0th param > node 1st param > test.js (file to run) 2nd > somevar 3rd > filename |
另一个错误:
最终代码将类似
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | const csv = require('fast-csv'); const fs = require('fs'); console.log("hello world") console.log("list of order prices", process.argv[2]); let required_products=[] for(var i=3;i<process.argv.length;){ let temp=[] temp.name=process.argv[i++] temp.quantity=process.argv[i++] required_products.push(temp) } /* To read csv file and print the data to the console: [node orderPrice --list input/sample.catalog.csv] */ let stream = fs.createReadStream(process.argv[2]); var total = 0; var csvStream = csv() .on("data", function(data){ let product_name = data[0]; let quantity = data[1]; let price = data[2]; required_products.forEach(function(product){ if(product['name']==product_name){ if(parseInt(product['quantity'])>parseInt(quantity)){ console.log('Quantity required for product '+product['name']+' '+product['quantity']+' is greater than available '+quantity); process.exit(1) }else{ total += parseInt(price) * parseInt(product['quantity']); } } }) }) .on("end", function(){ console.log("done"); let totalWithVat = total * (1+ 0.23); console.log("Total price:", totalWithVat); }).on("error", function (error) { console.error("The following error occured:", error); }) stream.pipe(csvStream); |
它听起来可能很油嘴滑舌,但是program.list没有定义,这就是为什么你不能从中读取它。它是未定义的,因为您没有设置指挥官来了解它如何从命令行映射。如果您查看指挥官文档中的示例,它可能会提供更多信息(并查看"变量参数"的帮助)。您可以按照下面的示例了解如何使用命令和操作方法。
我建议如果你刚开始,不要使用像指挥官这样的包,如果你不需要的话。你的问题真的是关于使用指挥官。这个问题提供了一些关于如何获取命令行参数的很好的提示。
具有以下目录结构:
- orderPrices.js
- sample.catalog.csv
您可以执行
如果您的csv文件位于其他位置。您可以使用