【发布时间】:2020-01-16 00:08:29
【问题描述】:
我在 AWS Lambda 中运行的 node.js (nodejs10.x) 中有此代码。
module.exports.register = (event, context, callback) => {
// Body es un json por lo que hay que deserializarlo
let body = JSON.parse(event.body);
let rules = {
'name': 'required|min:3',
'family_name': 'required|min:3',
'email': 'required|email',
'curp': 'required|size:18',
'modules': 'required',
'password': 'required'
};
let validation = new validator(body, rules);
// If errors this validation exits using the callback
if(validation.fails()){
console.log(validation.errors.all())
const response = {
statusCode: 422,
body: JSON.stringify(validation.errors.all())
};
callback(null, response);
}
// just for testing
const isModulesValid = false;
if(!isModulesValid){
console.log('Modules validation failed. ')
const response = {
statusCode: 422,
body: JSON.stringify({'modules': 'Invalid modules string. '})
};
callback(null, response);
// However this is not working
}
// and even if there is an error this code is executed
console.log('XXXX');
我正在使用这样的代码在本地对其进行测试。
// Callback
let callback = function(err, result) {
if (err)
console.log(err);
if (result)
console.log(result);
// Terminate execution once done
process.exit(0);
}
// lambda.generateToken(event, context, callback);
lambda.register(event, context, callback);
在本地,如果 isModulesValid = false 代码退出并且不执行 console.log('XXXX')。但是,在 AWS Lambda 中运行它时,即使验证失败,代码也会继续运行并执行 console.log()。
我不知道发生了什么。请帮忙?
【问题讨论】:
标签: node.js callback aws-lambda