Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Node JS:如果在Promise.all上读取多个文件,如何在读取文件时捕获单个错误?_Node.js_Fs_Readfile - Fatal编程技术网

Node.js Node JS:如果在Promise.all上读取多个文件,如何在读取文件时捕获单个错误?

Node.js Node JS:如果在Promise.all上读取多个文件,如何在读取文件时捕获单个错误?,node.js,fs,readfile,Node.js,Fs,Readfile,我有10个不同的文件,我需要读取它们的内容,并将其合并到一个对象中(在NodeJS中)。我通过以下代码成功地做到了这一点: const fs = require('fs'); const path = require('path'); const { promisify } = require("util"); const readFileAsync = promisify(fs.readFile); let filePathArray = ['path/to/fi

我有10个不同的文件,我需要读取它们的内容,并将其合并到一个对象中(在NodeJS中)。我通过以下代码成功地做到了这一点:

const fs = require('fs');
const path = require('path');
const { promisify } = require("util");    
const readFileAsync = promisify(fs.readFile);

let filePathArray = ['path/to/file/one', ... , 'path/to/file/ten'];
Promise.all(
  filePathArray.map(filePath => {          
    return readFileAsync(filePath);
  })
).then(responses => { //array of 10 reponses
  let combinedFileContent = {};
    responses.forEach((itemFileContent, index) => {
      let tempContent = JSON.parse(itemFileContent);
      //merge tempContent into combinedFileContent 
    }
});
但我想知道的是,如何在尝试读取文件时捕捉到错误?读取单个文件时,其工作原理如下:

fs.readFile(singleFilePath, (singleFileErr, singleFileContent) => {
  if (singleFileErr) {
    //do something on error, while trying to read the file        
  }
});
因此,我的问题是,如何访问第一个代码段中的错误,它对应于第二个代码段中的
singleFileErr

我面临的问题是:如果某些文件不存在,我想检查错误并跳过此文件,但由于我无法在当前实现中检测到错误,因此我的整个块崩溃,无法合并其他9个文件。我想使用我在第二个代码段中提到的错误检查。

检查该函数,它将运行传递给它的每个
Promise
,并在最后告诉您哪些成功,哪些失败。

检查该函数,它将运行传递给它的每个
Promise
,最后会告诉你哪些成功了,哪些失败了。

也许可以试试这样的方法:

Promise.all(
  filePathArray.map(filePath => {          
    return readFileAsync(filePath).catch(function(error){
      if(isErrorFileDoesNotExist(error)) return null
      throw error;
    })
  });
).then(responses => {
   return responses.filter(response => response != null) 
})
.then(filteredResponses => { 
  // .. do something
});
  • map()
    回调中,如果未找到文件,则返回解析为
    null
    的承诺
  • 在承诺链中引入一个中间阶段,过滤掉
    null
    响应
这看起来像这样:

Promise.all(
  filePathArray.map(filePath => {          
    return readFileAsync(filePath).catch(function(error){
      if(isErrorFileDoesNotExist(error)) return null
      throw error;
    })
  });
).then(responses => {
   return responses.filter(response => response != null) 
})
.then(filteredResponses => { 
  // .. do something
});

那对你有用吗?注意:这假定您实际上能够区分丢失的文件错误和其他错误,
readFileAsync()
返回的承诺可能会拒绝——可能是通过此代码段中的
isErrorFileDoesNotExist()
函数来拒绝的。

可以尝试以下操作:

Promise.all(
  filePathArray.map(filePath => {          
    return readFileAsync(filePath).catch(function(error){
      if(isErrorFileDoesNotExist(error)) return null
      throw error;
    })
  });
).then(responses => {
   return responses.filter(response => response != null) 
})
.then(filteredResponses => { 
  // .. do something
});
  • map()
    回调中,如果未找到文件,则返回解析为
    null
    的承诺
  • 在承诺链中引入一个中间阶段,过滤掉
    null
    响应
这看起来像这样:

Promise.all(
  filePathArray.map(filePath => {          
    return readFileAsync(filePath).catch(function(error){
      if(isErrorFileDoesNotExist(error)) return null
      throw error;
    })
  });
).then(responses => {
   return responses.filter(response => response != null) 
})
.then(filteredResponses => { 
  // .. do something
});
那对你有用吗?注意:这假定您实际上能够区分丢失的文件错误和其他错误,
readFileAsync()
返回的承诺可能会拒绝—可能是通过此代码段中的
isErrorFileDoesNotExist()函数来拒绝的