【问题标题】:Returning value from "finish" event after piping data from a read stream to a write stream在将数据从读取流传输到写入流之后从“完成”事件返回值
【发布时间】:2022-01-06 18:45:49
【问题描述】:

我正在尝试创建一个读取流,然后将 word 文档 XML 文件的内容通过管道传输到写入流,然后从完成的写入流中读取。我遇到的问题是,在第一个读取、写入和读取的序列中,我得到一个[Error: ENOENT: no such file or directory, open] 错误。但是,在第一次尝试创建此文件后,代码运行平稳并按预期返回 pageCount 值。 我试图从完成的文件中读取,然后在“完成”事件中返回 pageCount 值,但这只会给我留下一个未定义的返回值。因此,我不确定该怎么做。

对于这位苦苦挣扎的小辈,我们将不胜感激。

更新,以下代码对我有用。

  console.log("unzipping");
  const createWordOutput = new Promise((resolve, reject) => {
    console.log("end is firing");
    fs.createReadStream(data)
      .pipe(unzipper.Parse())
      .on("entry", async function (entry) {
        const fileName = entry.path;
        const type = entry.type;
        const size = entry.vars.uncompressedSize;
        //docProps has the meta data of the document, the word/document.xml is the actual document
        if (fileName === "docProps/app.xml") {
          // console.log(fileName);
          entry.pipe(fs.createWriteStream("./wordOutput")).on("finish", () => {
            console.log("finished writing the file");
            console.log("resolving");
            return resolve();
          });
          //once the piping is completed and the XML structure is fully writen a 'finish' event is emitted. This event accepts a callback. Here I put the cb and call readTheFile on the ./output file. This successfully reads the metadata of each file
        } else {
          entry.autodrain();
        }
      });
  });
  await createWordOutput;

  const 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;
};```

【问题讨论】:

    标签: node.js fs


    【解决方案1】:

    错误来自readWordFile,因为它在流完成之前运行。 您需要将阅读移至finish 部分

    试试这个:

    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 = (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);
        });
    };
    

    【讨论】:

    • 感谢您的回复!当我尝试这个时,我最终返回了一个未定义的值,我也不明白......
    • 所以 ENOENT 不见了?你是从哪里得到的,从“PAGECOUNT ..”日志,或者调用整个事情的东西..?如果你没有找到它,你可能会抛出readWordFile 代码,但我认为有些东西在它的时间之前运行..
    • 正确,它已经消失了,但我没有返回值。所以“完成”事件绝对适合等待文件写入,但我似乎无法访问其中的任何变量。
    • 你没有返回一个承诺.. 尝试更新的代码
    • 用新代码更新,它可以工作了,谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2023-03-29
    • 2023-04-05
    • 1970-01-01
    • 2011-02-12
    相关资源
    最近更新 更多