我需要使用node
在async
中运行一系列模块。我async.waterfall
嵌套在async.series
内,但在async.series
方法中遇到第二个函数时遇到问题。打印"Processing images completed"
后,该过程结束。这是在aws lambda
。
我做错了什么?
这是我的模块:
S3resizer.rs
S3resizer.rs = function (imgName, bucketName, sizesObj, imageType, obj, cb) {
var dir = "/tmp";
async.series([
function (next) {
async.waterfall([
function (callback) {
S3get(bucketName, imgName, callback);
},
function (data, callback) {
async.each(sizesObj, function (sizesObj, mapNext) {
rs(data, imgName, dir, sizesObj, mapNext);
}, function (err) {
if (err) {
console.log("Error when resizing images, %s", err);
callback(err);
} else {
console.log("Processing images completed");
callback();
}
});
}
], function (err) {
console.log("Fetched and resized ok!");
next(null);
});
},
function (next) {
async.waterfall([
function (callback) {
console.log("reached readfile");
readDirFile(dir, callback);
}
], function (error, results) {
if(error) {
next(error);
} else {
next(null, results);
}
});
}
], function (err, results) {
if (err) {
cb(err);
} else {
cb(null, results);
}
});
};
'rs'模块:
'use strict';
// dependencies
var gm = require('gm').subClass({ imageMagick: true });
var resizer = {};
resizer.resize = function (data, imgName, directory, sizesObj, callback) {
if (data.hasOwnProperty('Body')) {
data = data.Body;
} else {
data = data;
}
gm(data)
.resize(sizesObj.width, sizesObj.height)
.write(directory + "/" + sizesObj.name + "_" + imgName, function (err) {
if (err) {
console.error("Error resizing image, %s", err.message);
callback(err);
return;
}
console.log("Wrote to %s directory, with size name %s and image name %s", directory, sizesObj.name, imgName);
callback(null);
});
};
module.exports = resizer;
答案 0 :(得分:1)
这看起来不对:
if (err) {
console.log("Error when resizing images, %s", err);
} else {
console.log("Processing images completed");
callback;
}
首先,当发生错误时,您不会调用callback
。第二,你实际上并没有调用回调函数,因为它缺少括号(即callback()
)。