我一直在研究读取csv时输出xml的项目,我使用fs.createReadStream()
方法读取csv文件,但过了一段时间,终端崩溃了。
我得到
C:\Users\username\Documents\Programming\Node Projects\DAE Parser\main.js:13
row["Value"].includes("tri") ||
^
TypeError: Cannot read property 'includes' of undefined
它不会读取整个文件。
这就是我在做什么
fs.createReadStream("test.csv")
.pipe(csv())
.on("data", row => {
if (
row["Value"].includes("tri") ||
row["Value"].includes("vt") ||
row["Value"].includes("vx") ||
row["Value"].includes("vn")
) {
console.log(row)
}
})
您的row["Value"]
未定义,您可以添加条件来检查其是否虚假
fs.createReadStream("test.csv")
.pipe(csv())
.on("data", row => {
if (row["Value"] && (
row["Value"].includes("tri") ||
row["Value"].includes("vt") ||
row["Value"].includes("vx") ||
row["Value"].includes("vn")
)) {
console.log(row)
}
})
如果要在某些特定行中可能不是数组的情况下,要对row["Value"]
保持绝对的安全,则可以执行此操作:
fs.createReadStream("test.csv")
.pipe(csv())
.on("data", row => {
let arr = row["Value"];
if (arr && Array.isArray(arr) && (
arr.includes("tri") ||
arr.includes("vt") ||
arr.includes("vx") ||
arr.includes("vn")
)) {
console.log(row)
}
})