引导csv文件一段时间后NodeJS崩溃

我一直在研究一个在读取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)
    }
  })
wlst34 回答:引导csv文件一段时间后NodeJS崩溃

您的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)
    }
  })
,

在以下情况下,您的代码容易受到攻击:

  1. row不是对象
  2. row["Value"]不存在或不是数组。

如果您想在任何特定行中完全免受这些攻击,则可以执行以下操作:

fs.createReadStream("test.csv")
  .pipe(csv())
  .on("data",row => {
    if (typeof row === "object") {
      let arr = row.Value;
      if (arr && Array.isArray(arr) && (
        arr.includes("tri") ||
        arr.includes("vt") ||
        arr.includes("vx") ||
        arr.includes("vn")
      )) {
        console.log(row);
      }
   }
})
本文链接:https://www.f2er.com/3166341.html

大家都在问