简体   繁体   中英

Why is my Lambda callback not working?

I am running a AWS Cognito signin process authentication API on AWS SAM local. I am getting authed correctly from Cognito but when the signInUser promise resolves (with the correct response), instead of the callback with the statusCode of 200 getting fired, it fires the callback in the catch (with the statusCode of 400).

See Lambda function here:-

// 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})

    }
}

Any ideas what is causing this to happen or how to fix it?

Much thanks!

.catch() takes a function, but you are passing it the result of your callback. Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM