我正在尝试在AWS Lambda上实现一个简单的Node Js示例,
该代码具有Async库的示例示例。
该代码可以正常工作,但是由于某种原因Lambda函数返回空响应。
我也是Node的新手,请帮忙。
以下是代码-
var async = require('async');
exports.handler = async (event, context, callback) => {
async.waterfall([
function(callback) {
console.log("ONE");
callback(null, 1);
},
function(resp, callback) {
console.log("TWO : ", resp);
callback(null, 2);
},
function(resp, callback){
console.log("THREE : ", resp);
callback(null, "Done");
}
],
function(err, resp) {
let response = {};
if (err) {
console.log("Error",err);
response = {
statusCode: 500,
body: JSON.stringify('Error!'),
};
return response;
} else {
console.log("Success",resp);
response = {
statusCode: 200,
body: JSON.stringify('Ok!'),
};
return response;
}
});
};
以下是CloudWatch日志-
START RequestId: ab9aa426-dfc9-44ac-8d96-a4f102e30861 Version: $LATEST
2019-03-21T15:15:26.597Z ab9aa426-dfc9-44ac-8d96-a4f102e30861 ONE
2019-03-21T15:15:26.597Z ab9aa426-dfc9-44ac-8d96-a4f102e30861 TWO : 1
2019-03-21T15:15:26.597Z ab9aa426-dfc9-44ac-8d96-a4f102e30861 THREE : 2
2019-03-21T15:15:26.597Z ab9aa426-dfc9-44ac-8d96-a4f102e30861 Success Done
END RequestId: ab9aa426-dfc9-44ac-8d96-a4f102e30861
REPORT RequestId: ab9aa426-dfc9-44ac-8d96-a4f102e30861 Duration: 37.28 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 67 MB
我使用了示例节点蓝图,该蓝图很好用-
exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
答案 0 :(得分:3)
由于您已经在使用Node 8,因此不再需要使用过时的,令人困惑的X_
方法。请改用callback
await
其中somePromise1,somePromise2和somePromise3是您承诺的回调。
有关异步/等待here的更多信息。
答案 1 :(得分:1)
尝试从处理程序中删除exports.handler = async (event) => {
try {
await somePromise1
await somePromise2
await somePromise3
console.log("Success", resp);
response = {
statusCode: 200,
body: JSON.stringify('Ok!'),
};
return response;
} catch (e) {
console.log("Error", err);
response = {
statusCode: 500,
body: JSON.stringify('Error!'),
};
return response;
}
};
,并使用async
而不是返回值:
callback