Async.waterfall 函数在保存解析对象后退出
Async.waterfall function exiting after saving Parse object
我的aws-lambda函数中有一个异步函数,效果很好。
它这样做:
-
抓取图像("下载"功能),
-
裁剪并将其调整为缩略图("转换"功能),
-
将该缩略图上传到新存储桶("上传"功能),
-
使用指向该缩略图的 url 更新场地对象(这是一个解析对象)("updateVenue"函数)
-
最后,它创建一个新的场景对象(这也是一个解析对象)("saveScene"函数)。
**我省略了指定 venueObj 和 sceneObj 的代码,以使其更简单,因为我认为这不是问题所在。
我的问题是,在 updateVenue 函数记录为成功完成后,下一个日志是:进程在完成请求之前退出。 又名 saveScene 函数永远不会被调用。
即使我翻转 updateVenue 和 saveScene 函数的顺序,该过程也会在第一个 Parse 函数 - saveScene 完成后退出。 因此,我认为错误在于我称呼这些的方式。
我也在使用context.success(),也许这与它有关?
// Download the image from S3, transform, and upload to a different S3 bucket.
async.waterfall([
function download(next) {
// Download the image from S3 into a buffer.
s3.getObject({
Bucket: srcBucket,
Key: srcKey
},
next);
},
function transform(response, next) {
gm(response.Body).size(function(err, size) {
// Infer the scaling factor to avoid stretching the image unnaturally.
WIDTH = size.width;
HEIGHT = size.height;
if (WIDTH > HEIGHT) {
var side = HEIGHT;
}
else{
var side = WIDTH;
}
var scalingFactor = Math.min(
MAX_WIDTH / side,
MAX_HEIGHT / side
);
var width = scalingFactor * side;
var height = scalingFactor * side;
// Transform the image buffer in memory.
this.gravity("Center").crop(side, side).resize(width, height)
.toBuffer(imageType, function(err, buffer) {
if (err) {
next(err);
console.log(err);
} else {
next(null, response.ContentType, buffer);
}
});
});
},
function upload(contentType, data, next) {
// Stream the transformed image to a different S3 bucket.
s3.putObject({
Bucket: dstBucket,
Key: dstKey,
Body: data,
ContentType: contentType
},
next);
},
function updateVenue(next) {
venueObj.save(null, {
success: function(response){
console.log('Updated Venue thumbnail succesfully: ', response);
context.succeed();
next
},
error: function(response, error){
console.log('Failed to update Venue thumbnail, with error code: ' + error.description);
context.fail();
next
}
}); // end of venueObj.save
},
function saveScene(next) {
sceneObj.save(null, {
success: function(response){
console.log('Saved sceneObj succesfully: ', response);
context.succeed();
next
},
error: function(response, error){
console.log('Failed to create new sceneObj, with error code: ' + error.description);
context.fail();
next
}
}); // end of sceneObj.save
}
], 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");
}
);
我相信你只需要在updateVenue中调用next
并保存Scence。 async.waterfall 将回调传递给系列中的每个函数,您当前正在使用 next
。 如果需要将数据传递给下一个 fn,请将其作为第二个参数传递给回调。
以下是如何在updateVenue
中应用的示例:
function updateVenue(next) {
return venueObj.save(null, {
success: function(response){
console.log('Updated Venue thumbnail succesfully: ', response);
return next(null, response);
},
error: function(response, error){
console.log('Failed to update Venue thumbnail, with error code: ' + error.description);
return next(error);
}
}); // end of venueObj.save
},...
希望对您有所帮助!
我找到了解决这个问题的方法。不幸的是,我无法将函数分开,但是,我能够将第二个函数嵌入到第一个函数的完成块中:
// Download the image from S3, transform, and upload to a different S3 bucket.
async.waterfall([
function download(next) {
// Download the image from S3 into a buffer.
s3.getObject({
Bucket: srcBucket,
Key: srcKey
},
next);
},
function transform(response, next) {
gm(response.Body).size(function(err, size) {
// Infer the scaling factor to avoid stretching the image unnaturally.
WIDTH = size.width;
HEIGHT = size.height;
if (WIDTH > HEIGHT) {
var side = HEIGHT;
}
else{
var side = WIDTH;
}
var scalingFactor = Math.min(
MAX_WIDTH / side,
MAX_HEIGHT / side
);
var width = scalingFactor * side;
var height = scalingFactor * side;
// Transform the image buffer in memory.
this.gravity("Center").crop(side, side).resize(width, height)
.toBuffer(imageType, function(err, buffer) {
if (err) {
next(err);
console.log(err);
} else {
next(null, response.ContentType, buffer);
}
});
});
},
function upload(contentType, data, next) {
// Stream the transformed image to a different S3 bucket.
s3.putObject({
Bucket: dstBucket,
Key: dstKey,
Body: data,
ContentType: contentType
},
next);
},
function updateVenue(next) {
venueObj.save(null, {
success: function(response){
console.log('Updated Venue thumbnail succesfully: ', response);
sceneObj.save(null, {
success: function(response){
console.log('Saved sceneObj succesfully: ', response);
context.succeed();
next
},
error: function(response, error){
console.log('Failed to create new sceneObj, with error code: ' + error.description);
context.fail();
next
}
}); // end of sceneObj.save
},
error: function(response, error){
console.log('Failed to update Venue thumbnail, with error code: ' + error.description);
context.fail();
next
}
}); // end of venueObj.save
}
], 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");
}
);
相关文章:
- 引用对象中的通用值
- jQuery匹配JSON对象的部分文本
- 节点导出返回一个空对象
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 全局变量和全局对象的属性之间有什么区别吗
- 比较从函数和生成的日期对象
- Javascript,访问一个主要对象模块模式中的每个对象
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 调整窗口大小时,可拖动的对象会出现在容器外部
- 如何使用javascript从主svg对象动态创建svg视图框
- 如何使用json将对象列表从java转换为javascript
- Async.waterfall 函数在保存解析对象后退出
- 如何单击未完全覆盖背景的对象,并在悬停时消失,同时通过窗口对象退出元素
- 退出工作中的对象方法
- 停止firefox退出延迟对象的执行
- 如何从退出一个创建新的json对象
- D3 -对象常量,键函数和(更新,进入,退出)
- 在数组中添加/删除对象,并检查对象是否已退出