我的aws-lambda函數內部有一個異步函數,效果很好。保存Parse對象後退出Async.waterfall函數
它這樣做是:
抓取的圖像( '下載' 功能),
作物,它會調整到縮略圖( '轉換' 功能),
上傳該縮略圖到一個新的桶('上傳'功能),
更新場地對象(這是一個Parse對象)與URL縮略圖('updateVenue'功能)
最後,它創建一個新的場景對象(這也是一個解析對象)('saveScene'函數)。
**我遺漏了指定場景對象和場景對象的代碼,使其更簡單,因爲我不相信這是問題所在。
我的問題是,在成功完成updateVenue函數記錄之後,下一個日誌是:進程在完成請求之前退出。也就是說saveScene函數永遠不會被調用。
即使當我翻轉updateVenue和saveScene函數的順序時,該過程在第一個Parse函數 - saveScene完成後退出。因此,我相信這個錯誤是我以這種方式調用的。
此外,我正在使用context.succeed(),也許這與它有什麼關係?
// 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");
}
);
它說,返回下一個(空,響應);不是一個功能:/ – ian