2016-04-22 48 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