2016-04-22 45 views
0

我是新來的節點和回調。現在,我正在使用async.waterfall來混淆視頻,但由於某種原因,我在我的瀑布中插入第二個函數「pipe」後退出了此過程。我沒有正確調用它嗎?Async.waterfall不會去下一個功能

// Download the video from S3, get thumbnail, and upload to a different S3 bucket. 
     async.waterfall([ 
      function download(next) { 
       // Download the video from S3 into a buffer. 
       s3.getObject({ 
         Bucket: srcBucket, 
         Key: srcKey 
       }, 
       next); 
      }, 
      function pipe(next) { 
       // Download the video from S3 into a buffer. 
       console.log("pipe function started"); 
       var params = {Bucket: srcBucket, Key: srcKey}; 
       s3.getObject(params).createReadStream().pipe(file, next); 
      }, 
      function upload(response, next) { 
       console.log("upload function started"); 
       // Stream the transformed image to a different S3 bucket. 
       s3.putObject({ 
         Bucket: dstBucket, 
         Key: dstKey, 
         Body: response.Body, 
         ContentType: response.ContentType 
        }, 
        next); 
      } 
      ], function (err) { 
       if (err) { 
        console.error(
         'Unable to resize ' + srcBucket + '/' + srcKey + 
         ' and upload to ' + dstBucket + '/' + dstKey + 
         ' due to an error: ' + err 
        ); 
       } else { 
        console.log(
         'Successfully resized ' + srcBucket + '/' + srcKey + 
         ' and uploaded to ' + dstBucket + '/' + dstKey 
        ); 
       } 

       callback(null, "message"); 
      } 


     ); 
+0

'.pipe'是否需要回撥?我希望你必須註冊'next'作爲可讀流'end'事件的處理函數 – andyk

回答

2

將帖子

你說你插入pipe功能。所以,原來upload(response, next)函數是在download函數之後調用的。鑑於upload函數的簽名,我們可以推測download函數大致以這種方式調用它的next函數:next(null, response)。因此,在瀑布陣列中的download函數之後的任何函數都將傳遞2個參數:(response, next)

有關如何在任務之間傳遞數據的詳細信息,請參閱waterfall的文檔。

所以,你的代碼的直接問題是pipe()實際上被傳遞了2個參數:(response, next),但是你的代碼只定義了一個參數:(next)。因此,它試圖使用response參數(一個對象),就好像它是一個函數。

您還有其他問題,但如何解決這些問題取決於您試圖達到的目標。

+0

我在哪裏將'response'傳遞給'pipe'? – ian

+0

看到我更新的答案。 – cybersam