2016-10-05 9 views
0

async.eachを使用して、fs.statを使用して180個のファイル名の配列をチェックし、物理ファイルが存在することを確認しています。現在のところ、すべてのファイルは存在していますが、このコードを呼び出すたびに、fs.statによって180イメージのうち3イメージが存在しないことがわかります。見つからなかった画像は、呼び出しごとにわずかに異なります。fs.statが既存のファイルを特定できません

最初に見つからないイメージは、常に配列の70番目のファイル名の周りにあります。配列のサイズを(たとえば)50個の名前のリストに減らすと、すべてのイメージが見つかります。

今async.forEachOfで、代わりにfs.statのfs.open使用して、基本的には同じ結果になっ
// ImagesToCheck is an array of 180 filepaths, all of which exist on the server 

var index = 0 

async.each(ImagesToCheck, 
function(item, cb) { 

    fs.stat(item, function(error, value) { 
     if (error) { 
      // Logs "ENOENT: no such file or directory" 
      console.log("error is "+error) 
      // this is logging the correct filename, which DOES exist 
      console.log("filename is "+ImagesToCheck[index]) 

      Speakers[index].speaker_image_url = null 
      index++ 
      cb() 
     } 
     else { 
      // Image exists 
      // console.log("image exists " + index) 
      Speakers[index].speaker_image_url = "http://www.apiurl.co.uk/images/seminar_image-" + rows[index].seminar_id + ".jpg" 
      index++ 
      cb() 
     } 
    }) // close fs.stat 
}, 
function(err) { 
    // sends the array of URLs 
    callBack(Speakers) 
}) 

//:

var index = 0 

async.forEachOf(ImagesToCheck, 
    function(item, key, cb) { 

     fs.open(item, 'r', function(error, value) { 
      if (error) { 
       // Logs "ENOENT: no such file or directory" 
       console.log("error is " + error) 
        // this is logging the correct filename, which DOES  exist 
       console.log("index is " + index + ", filename is " + ImagesToCheck[index]) 
       Speakers[index].speaker_image_url = null 
       index++ 
       return cb() 
      } 

      // if no error 
      Speakers[index].speaker_image_url = "http://www.apiurl.co.uk/images/seminar_image-" + rows[index].seminar_id + ".jpg" 
      index++ 
      cb() 
     }) 
    }, 
    function(err) { 
    // send the array of urls 
    callBack(Speakers) 
    } 
) 
+0

このインデックスは、印刷前に更新される可能性があるため、今のところ有用な情報を提供しません。 'async.eachOf'を使って実インデックスを取得します – DrakaSAN

+0

forEachOfに変更しましたが、それと同じように動作します! ... –

+0

200個のファイルをチェックするときにテスト実装を行いました。エラーはありません – DrakaSAN

答えて

0

注:これは単なるテストの実装を示すことです。答え

ちょうどfilesという名前のフォルダに200個のファイル(0.txt、1.TXT ... 200.txt)を生成しようとしましたが、このコードを試してみましたので、これは意図されていない:

'use strict'; 

const fs = require('fs'), 
    async = require('async'); 

let files = [], 
    results = [], 
    i = 0; 

while(i < 200) { 
    files.push('./files/' + i + '.txt'); 
    i = i + 1; 
} 

async.eachOf(files, (file, i, callback) => { 
    fs.stat(file, (err, stats) => { 
     files[i] = !err; 
     callback(); 
    }); 
},() => { 
    let notFound = files.filter((f) => { 
     return !f; 
    }); 
    console.log('fs.stat didn\'t found ' + notFound.length + '/200 files'); 
    files.map((f, i) => { 
     if(!f) { 
      console.log('File ' + i + ' wasn\'t found'); 
     } 
    }) 
}); 

エラーは見つかりませんでした。問題がないようです。fs.stat

+0

私は上記を試しましたが、それはファイルのいずれも見つかりません。 「ファイルが見つかりません」がそれぞれ記録されます。私は、絶対ファイルパスを使用して、元のAPIからのファイルと同じディレクトリに配置しました。また、リクエストが "ハングする" - アプリケーション全体がクラッシュしない、私はあなたの例のために使用している特定のURLは、追加のエラーが記録されていない年齢のためにハングアップします。 –

+0

'filesToCheck'の例を投稿できますか?また、問題は 'fs.stats'にあるので、実際のコードからURLを検索せずにMCVEを構築し、エラーがどこにあるのかを理解し、正しいコードを実際のアプリケーションに戻すことをお勧めします。 – DrakaSAN

+0

ごめんなさいfilesToCheckは何を参照していますか? –

関連する問題