【问题标题】:Why is my Lambda callback not working?为什么我的 Lambda 回调不起作用?
【发布时间】:2017-11-15 01:37:04
【问题描述】:

我正在本地 AWS SAM 上运行 AWS Cognito 登录过程身份验证 API。我从 Cognito 获得了正确的身份验证,但是当 signInUser 承诺解决时(使用正确的响应),而不是触发 statusCode 为 200 的回调,它触发了 catch 中的回调(statusCode 为 400)。

在此处查看 Lambda 函数:-

// A signin Lambda function
export function handler (event: Object, context: Object, callback: Function) {    
    switch (event.httpMethod) {
        case "GET":
            // hard code login for SO question
            signInUser({ username: 'XXXX', password: 'XXXXXXX'})
                .then((response) => { 
                    console.log('This log is called correctly but callback on the next line is not');
                    callback(null, {
                        statusCode: 200,
                        header: response.tokens.idToken.jwtToken,
                        body: "This is a signin operation, return success result"
                    });
                 })
                .catch(
                    callback(null, {
                        statusCode: 400,
                        body: "This is a failed signin operation"
                    })
                );
            break;
        default:
            // Send HTTP 501: Not Implemented
            console.log("Error: unsupported HTTP method (" + event.httpMethod + ")");
            callback(null, {statusCode: 501})

    }
}

任何想法是什么导致这种情况发生或如何解决?

非常感谢!

【问题讨论】:

  • 您将callback 函数传递给catch 的方式是错误的。

标签: javascript amazon-web-services lambda es6-promise


【解决方案1】:

.catch() 接受一个函数,但您将回调的结果传递给它。试试这个:

.catch( (error) => 
    callback(null, {
        statusCode: 400,
        body: "This is a failed signin operation"
    })
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2015-08-14
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    相关资源
    最近更新 更多