简体   繁体   English

在从读取 stream 到写入 stream 的管道数据之后,从“完成”事件返回值

[英]Returning value from "finish" event after piping data from a read stream to a write stream

I am trying to create a read stream and then pipe the contents of a word document XML file to a write stream and then read from that finished write stream. I am trying to create a read stream and then pipe the contents of a word document XML file to a write stream and then read from that finished write stream. The problem I am running into is that on the first sequence of reading then writing and then reading I get a [Error: ENOENT: no such file or directory, open] error.我遇到的问题是,在读取然后写入然后读取的第一个序列中,我得到一个[Error: ENOENT: no such file or directory, open]错误。 However after this file was created from the first attempt the code runs smoothly and returns the pageCount value as expected.但是,在第一次尝试创建此文件后,代码运行平稳并按预期返回 pageCount 值。 I have tried to read from the completed file and then return the pageCount value inside of the 'finish' event, but that just leaves me with an undefined returned value.我试图从完成的文件中读取,然后在“完成”事件中返回 pageCount 值,但这只会给我留下一个未定义的返回值。 As such, I am not sure what to do.因此,我不确定该怎么做。

Any help would be appreciated for this struggling junior.对于这个苦苦挣扎的小辈,我们将不胜感激。

  console.log("unzipping");
  let pageCount = "";
  fs.createReadStream(data)
    .pipe(unzipper.Parse())
    .on("entry", function (entry) {
      const fileName = entry.path;
      const type = entry.type;
      const size = entry.vars.uncompressedSize;
      if (fileName === "docProps/app.xml") {
        // console.log(fileName);
        entry.pipe(fs.createWriteStream("./wordOutput")).on("finish", () => {
          console.log("finished writing the file");
        });
      } else {
        entry.autodrain();
      }
    });
  pageCount = await readWordFile("./wordOutput");
  if (pageCount === undefined) {
    console.log("PAGECOUNT IS UNDEFINED");
  }
  console.log("logging page count in unzip the file");
  console.log(pageCount);
  return pageCount;
};

const readWordFile = async (data) => {
  console.log("this is the data that readWordFile received");
  console.log(data);
  console.log("reading word file");
  const XMLData = await fsp.readFile(data, { encoding: "utf-8" });
  console.log(XMLData);
  const pageCount = XMLData.split("<Pages>")
    .join(",")
    .split("</Pages>")
    .join(",")
    .split(",")[1];
  console.log("getting page count from read word file");
  console.log(pageCount);
  return pageCount;
};

The error is coming from readWordFile , because it runs before the stream is done.错误来自readWordFile ,因为它在 stream 完成之前运行。 You need to move reading to the finish part您需要将阅读移至finish部分

Try this:尝试这个:

console.log("unzipping");
let pageCount = "";
fs.createReadStream(data)
    .pipe(unzipper.Parse())
    .on("entry", function(entry) {
        const fileName = entry.path;
        const type = entry.type;
        const size = entry.vars.uncompressedSize;
        if (fileName === "docProps/app.xml") {
            // console.log(fileName);
            entry.pipe(fs.createWriteStream("./wordOutput")).on("finish", async() => {
                console.log("finished writing the file");

                // finished writing, do everything here, and return
                pageCount = await readWordFile("./wordOutput");
                if (pageCount === undefined) {
                    console.log("PAGECOUNT IS UNDEFINED");
                }
                console.log("logging page count in unzip the file");
                console.log(pageCount);
                return pageCount;

            });
        } else {
            entry.autodrain();
        }
    });

};



const readWordFile = async(data) => {
    return new Promise(async(resolve, reject) => {

        console.log("this is the data that readWordFile received");
        console.log(data);
        console.log("reading word file");
        const XMLData = await fsp.readFile(data, {
            encoding: "utf-8"
        });
        console.log(XMLData);
        const pageCount = XMLData.split("<Pages>")
            .join(",")
            .split("</Pages>")
            .join(",")
            .split(",")[1];
        console.log("getting page count from read word file");
        console.log(pageCount);
        resolve(pageCount);
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM