节点循环功能代码由于未定义数据而失败

时间:2019-08-05 14:55:50

标签: javascript node.js

我正在对某些数据使用节点运行一些简单的JS,以对其进行基本转换。

当我通过函数一次运行数据时,它可以按预期工作,但是当我再次运行它以进行另一次转换时,由于我再次发送或部分发送的数据似乎会引发JS错误。成为undefined,但我不知道为什么!

我的JS文件具有以下代码。它所做的就是获取输入数据,找到诸如<span style="font-size: 24px">Some Text</span>之类的HTML,隔离它,获取Some Text,将其包装在======中,然后将HTML替换为输入数据中的HTML。转换后的代码。

我的输入数据包含许多我想清除/转换的HTML实例。

// input & output
var readline = require("readline");
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
var i = 0;
var strings = [];

// HTML cleaner
function processHTML(line, openingTag, closingTag, mdStart, mdEnd) {
  var openTagIndex = line.indexOf(openingTag);
  var closeTagIndex = line.indexOf(closingTag);
  var closeTagLength = closingTag.length;
  var htmlTag = line.substring(openTagIndex, closeTagIndex + closeTagLength);

  var content = htmlTag.split(openingTag)[1];
  content = content.split(closingTag)[0];

  return line.replace(htmlTag, mdStart + content + mdEnd);
}

// process left over HTML
function processLine(line) {
  if (line.indexOf("font-size: 24px;") > 0) {
    // heading 1
    return processHTML(
      line,
      '<span style="font-size: 24px;">',
      "</span>",
      "======",
      "======"
    );
  }

  return line;
}

// loop through lines
rl.on("line", function(line) {
  strings[i] = line;
  i++;
}).on("close", function() {
  var data = strings.join("");

  data = processLine(data);
  // data = processLine(data);

  process.stdout.write(data);
});

我的输入数据如下:

<div id="en-note"><div>Sunday 5.6.16</div><div><br/></div><div>Money &amp; charity services I need to stop before leaving.</div><div><br/></div><div>Do this during or at latest by end of June.</div><div><br/></div><div><span style="font-size: 24px;">Services</span></div><ol><li><div>Netflix <span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">???</span></div></li><ol><li><div><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">cancel</span></div></li></ol><li><div>US DNS <span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">???</span></div></li><ol><li><div><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">cancel</span></div></li></ol><li><div>Drama Fever <span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">???</span></div></li><ol><li><div><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">cancel</span></div></li></ol><li><div>Crunchy Roll</div></li><ol><li><div><span style="font-weight: bold;">CANCELLED</span></div></li></ol></ol><div><br/></div><div><span style="font-size: 24px;">Christian</span></div><ol><li><div>City Bible Forum $100 support</div></li><ul><li><div><span style="font-weight: bold;">DONE</span> - June 4 &gt; <span style="font-weight: bold;">CANCELLED, TAX RECEIPT </span></div></li><li><div>&quot;ecom Sydney&quot;</div></li></ul><li><div>Youth For Christ $100 support</div></li><ul><li><div><span style="font-weight: bold;">DONE</span> - June 20 &gt; <span style="font-weight: bold;">CANCELLED, TAX RECEIPT </span></div></li><li><div>&quot;YOUTH FOR CHRIST AUSTR MITCHAM VIC&quot;</div></li></ul><li><div>Phil ministry $100 support</div></li><ul><li><div><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">FINISH after September payment</span></div></li><li><div>&quot;power to change&quot;</div></li></ul><li><div>Scripture Union</div></li><ul><li><div><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;"><span style="background-color: rgb(255, 250, 165); font-weight: bold;-evernote-highlight:true;">STOP</span></span> sending, not in country (donate $200 for all their efforts)</div></li></ul><li><div>Melbourne City Mission</div></li><ol><li><div><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;"><span style="background-color: rgb(255, 250, 165); font-weight: bold;-evernote-highlight:true;">STOP</span></span> sending, not in country</div></li></ol></ol><div><br/></div><div><span style="font-size: 24px;">Charities</span></div><ol><li><div>Save the Children</div></li><ul><li><div><span style="background-color: rgb(255, 

第一个data = processLine(data);运行良好。如果您看到其下方的注释行,则运行该命令后,我立即遇到JS错误,并说了类似you can't .split() undefined之类的内容,但我不知道为什么数据为undefined

抛出错误后,结果就出来了(我暂时禁用content = content.split(closingTag)[0];以查看发生了什么,这就是为什么您看到======Services</span>======的原因):

<div id="en-note"><div>Sunday 5.6.16</div><div><br/></div><div>Money &amp; charity services I need to stop before leaving.</div><div><br/></div><div>Do this during or at latest by end of June.</div><div><br/></div><div>======Services</span>======undefined======<span style="font-size: 24px;">Christian</span></div><ol><li><div>City Bible Forum $100 support</div></li><ul><li><div><span style="font-weight: bold;">DONE</span> - June 4 &gt; <span style="font-weight: bold;">CANCELLED, TAX RECEIPT </span></div></li><li><div>&quot;ecom Sydney&quot;</div></li></ul><li><div>Youth For Christ $100 support</div></li><ul><li><div><span style="font-weight: bold;">DONE</span> - June 20 &gt; <span style="font-weight: bold;">CANCELLED, TAX RECEIPT </span></div></li><li><div>&quot;YOUTH FOR CHRIST AUSTR MITCHAM VIC&quot;</div></li></ul><li><div>Phil ministry $100 support</div></li><ul><li><div><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">FINISH after September payment</span></div></li><li><div>&quot;power to change&quot;</div></li></ul><li><div>Scripture Union</div></li><ul><li><div><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;"><span style="background-color: rgb(255, 250, 165); font-weight: bold;-evernote-highlight:true;">STOP</span></span> sending, not in country (donate $200 for all their efforts)</div></li></ul><li><div>Melbourne City Mission</div></li><ol><li><div><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;"><span style="background-color: rgb(255, 250, 165); font-weight: bold;-evernote-highlight:true;">STOP</span></span> sending, not in country</div></li></ol></ol><div><br/></div><div><span style="font-size: 24px;">Charities</span></div><ol><li><div>Save the Children</div></li><ul><li><div><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;"><span style="background-color: rgb(255, 250, 165); font-weight: bold;-evernote-highlight:true;">STOP</span></span> sending, not in country</div></li></ul><li><div>Olivia Newton-John Foundation</div></li><ul><li><div><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;"><span style="background-color: rgb(255, 250, 165); font-weight: bold;-evernote-highlight:true;">STOP</span></span> sending, not in country</div></li></ul><li><div>Cancer Council $35 support</div></li><ul><li><div><span style="font-weight: bold;">DONE</span> - June 15 &gt; <span style="font-weight: bold;">CANCELLED, TAX RECEIPT </span></div></li></ul></ol><div><b><br/></b></div><div><b><br/></b></div><div><b><br/></b></div><div><b><br/></b></div><div><b><br/></b></div></div>

我浏览了一下,基本上在第一次HTML转换后,一堆数据变得不确定,然后其余数据保持不变。

抱歉,它有点长,如果将代码保存在几个文件中,您应该可以运行它。

不知道我是否已经在这部片中出演了太久而看不到明显的内容。非常感谢!

PS:我是Node的新手,我可能会错过一些与节点在基本级别上起作用的方式有关的真正明显的东西。我已经读了一点,但是主要是一点点地解决我当前的任务。

1 个答案:

答案 0 :(得分:0)

您只需要使用if语句检查索引来处理该行一次。基本上,您将所有内容压缩到单个line中,并检查它是否具有单个font-size: 24px;实例,对其进行处理,然后返回所有内容。

相反,您应该逐行查找font-size: 24px,如有必要,请更改行,然后返回所有内容。

以下代码可以做到这一点(出于示例目的,当它看到字符串close时,它将退出readline。)

// input & output
var readline = require("readline");
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});
var i = 0;
var strings = [];

// HTML cleaner
function processHTML(line, openingTag, closingTag, mdStart, mdEnd) {
    var openTagIndex = line.indexOf(openingTag);
    var closeTagIndex = line.indexOf(closingTag);
    var closeTagLength = closingTag.length;
    var htmlTag = line.substring(openTagIndex, closeTagIndex + closeTagLength);

    var content = htmlTag.split(openingTag)[1];
    content = content.split(closingTag)[0];

    return line.replace(htmlTag, mdStart + content + mdEnd);
}

// process left over HTML
function processLine(lines) {
    return lines
        .map(line =>
            line.includes("font-size: 24px;")
                ? processHTML(
                        line,
                        '<span style="font-size: 24px;">',
                        "</span>",
                        "======",
                        "======"
                  )
                : line
        )
        .join("");
}

// loop through lines
rl.on("line", function(line) {
    if (line === "close") rl.close();
    strings[i] = line;
    i++;
}).on("close", function() {
    var data = strings;

    data = processLine(data);

    process.stdout.write(data);
});