javascript - Node.js TypeError:路径必须是字符串或缓冲区
问题描述
我正在编写一个命令行程序,它使用 CSV 文件中的信息计算订单的总价格。
sample.catalog.csv 中的数据:
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 P2 4
(P4 6 P10 5 P12 1 是 csv 文件中提供的产品和数量)
总计:4151,25
这就是我目前所拥有的:
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);
})
我有以下错误:
λ 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.<anonymous> (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 新手,非常感谢任何帮助。谢谢你。
解决方案
换线
let stream = fs.createReadStream(program.list)
到
let stream = fs.createReadStream(program.argv[some number])
其中一些数字是您提到文件名的位置
例如使用以下命令运行程序
node test.js somevar filename
那么某个数字 = 3
0th param > node
1st param > test.js (file to run)
2nd > somevar
3rd > filename
另一个错误:
最终代码看起来像
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);
推荐阅读
- python-3.x - 使用熊猫获取数据框的第二列
- android - 如何在android中使用Volley发送对象的JSON数组
- r - 使用 sapply 在 R 中查找条件期望
- mysql - 在 JPA 查询中应用价格过滤器
- javascript - Javascript函数的问题没有在带有if条件的for循环中返回总和
- python - 在 PyMongo 中用“.”更新。在关键
- laravel-8 - Laravel 8 工厂类在创建工厂时不会覆盖参数
- java - 在 Android 应用程序空闲后,Firebase 没有获得新数据
- python - 如何从 Instagram 上的个人资料转到最近的帖子
- java - 为什么这个 HashSet 在打印时看起来是排序的?