为什么我的Lambda回调不起作用?

时间:2017-11-14 17:28:48

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

我在AWS SAM本地运行AWS Cognito登录流程身份验证API。我从Cognito正确地获得了authed,但是当signInUser promise解析时(使用正确的响应),而不是触发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})

    }
}

是什么原因导致这种情况发生或如何解决?

非常感谢!

1 个答案:

答案 0 :(得分:1)

.catch()接受一个函数,但是你传递回调的结果。试试这个:

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