2017-01-03 7 views
0

質問には、async.whilstが1回だけ呼び出されることが記載されています。しかし、2つのオブジェクト(長さ2)を含む配列を渡すと、配列のインデックスごとに1回ではなく1回呼び出されます。ここnodejs async.whilstは1回だけ起動します

//if previous awsKeys detected in req.body/images are detected : delete them. 
    exports.multipleDelete = function (req, res, next) { 
     var body = req.body; 
     //if object already has existing keys, they will be in the body.existingKeys array. 
     var awsKeyTrash = body.existingKeys; 
     if (awsKeyTrash !== undefined) { 
      var j = 0; 
      async.whilst(
       function() { 
        return j < awsKeyTrash.length; 
       }, 
       function() { 
        var picInfo = awsKeyTrash[j]; 
        s3.deleteObject({ 
         Bucket: aws.bucket, 
         Key: picInfo.key 
        }, function (err, data) { 
         if (err) { 
          console.log('delete err', err); 
        //if there is an error, set pic information to original 
          req.body[picInfo.name] = picInfo.picUrl; 
          req.body[picInfo.key] = picInfo.awsKeyVal; 
          j++; 
         }; 
         console.log('deleted') 
         console.log('j ', j) 
         j++; 
         res.send('deleted'); 
        }); 
       }, 
       function (err) { 
      console.log('profile edit , pic delteion err : ', err); 
      return res.status(409).send({ 
       message: ['Unable to edit profile at this time. Try again'] 
      }); 
       }) 
      next(); 
     } 
     else { 
      next(); 
     } 
    } 

はbody.existingKeys配列の例です:

 Array[ 
     { 
     awsKeyVal: "users/66085aa8-6501-4f90-973c-1b18edf4087eScreenShot2016-12-05at10.03.07PM.png", 
     key: "awsPictureKey", 
     name: "picture", 
     picUrl: "https://example-bucket.s3.amazonaws.com/users/66085aa8-6501-4f90-973c-1b18edf4087eScreenShot2016-12-05at10.03.07PM.png" 
    }, 
     { 
     awsKeyVal: "coverphoto/7180d1ae-748c-4b96-86cb-5cb29cebdc9bScreenShot2016-12-10at3.13.18PM.png", 
     key: "awsCoverKey", 
     name: "cover", 
     picUrl: "https://example-bucket.s3.amazonaws.com/coverphoto/7180d1ae-748c-4b96-86cb-5cb29cebdc9bScreenShot2016-12-10at3.13.18PM.png" 
    }] 

答えて

1

async.whylistは、あなたが呼び出しされていないコールバックを取ります。

next()も間違っているようです。

さらに、最後の関数はエラーハンドラではありません。最初の引数としてエラーを受け取ります。エラーがない場合はnullになることがあります。

async.whilst(
    function() { 
     return j < awsKeyTrash.length; 
    }, 
    function (callback) { 
     var picInfo = awsKeyTrash[j]; 
     s3.deleteObject({ 
      Bucket: aws.bucket, 
      Key: picInfo.key 
     }, function (err, data) { 
      if (err) { 
       console.log('delete err', err); 
       //if there is an error, set pic information to original 
       req.body[picInfo.name] = picInfo.picUrl; 
       req.body[picInfo.key] = picInfo.awsKeyVal; 
       j++; 
      }; 
      console.log('deleted') 
      console.log('j ', j) 
      j++; 
      res.send('deleted'); 

      callback(); 
     }); 
    }, 
    function (err) { 

     if(err) { 
      console.log('profile edit , pic delteion err : ', err); 
      return res.status(409).send({ 
       message: ['Unable to edit profile at this time. Try again'] 
      }); 
     } 

     // If someone else handles errors, pass it on here. Otherwise, 
     // throw or something else to block program flow 
     next(err); 
    } 
); 
+0

意味があります。あなたのエラー処理のコメントについては、if(err)条件ブロック内の戻り値はプログラムの流れを妨害しないでしょうか?私はそれが別の問題であるので、あなたが信用を得ることを望むならば、別の質問をすることができます。再度、感謝します。 – NoobSter

+1

それは異なります。これは非同期関数の中にあるので、res.sendの結果を親ハンドラに返すだけです。おそらくあなたが望むものではないでしょう。 –

関連する問題