AWS Lambda NodeJs无法返回响应

4

我正在尝试在 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;
};

你是如何调用 Lambda 的? - gillyhl
这里使用异步库 async.js 进行异步操作,是否会与原生异步操作产生冲突呢? - Alexander Staroselsky
我使用了默认参数,但是我的代码没有处理它们,只是简单地使用异步库并应该返回“response”对象。 - Ani
@AlexanderStaroselsky我应该从处理程序中删除async吗?第一次在Lambda上使用Node :/ - Ani
@AniruddhaRaje 如果您将lambda作为事件调用,则即使函数体中有返回值,它也不会返回任何内容。 - gillyhl
显示剩余2条评论
2个回答

6

既然您已经在使用Node 8,就不需要再使用过时、令人困惑的callback方法了。改用await方法。

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 (err) {
        console.log("Error", err);
        response = {
            statusCode: 500,
            body: JSON.stringify(err),
        };
        return response;
    }
};

其中 somePromise1、somePromise2 和 somePromise3 是您的已转换为 Promise 的回调函数。

有关 async/await 的更多信息,请单击此处


1
但是 Promise.all 并行启动所有 Promise(我相信),而 async.waterfall 在一个接一个地启动每个 Promise,并将结果传递给下一个。但我同意现在使用 async/await 是正确的选择,与像 async.js 这样的库相比(尽管名称相似,但完全不同!)。 - Jeremy Thille
最初我将这篇文章中的代码写成了 await promise1 await promise2await promise3,因为我认为 Waterfall 会按顺序依次运行它们。然后我想:"不对啊,这肯定是并行执行",所以我改成了 Promise.all。接着你发表了评论,我查看了文档,发现你说得对,这实际上是顺序执行的,所以我又改回来了,变成了三个 await。感谢你提供的意见! - Thales Minussi
非常感谢您,先生! :) - Thales Minussi

3

尝试从处理程序中删除async关键字,并使用callback代替return

var async = require('async');

exports.handler = (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);
            callback(null, {
                statusCode: 500,
                body: 'Error!',
            });
        } else {
            console.log("Success",resp);
            callback(null, {
                statusCode: 200,
                body: 'Ok!',
            });
        }
    });
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接