Monday 12 June 2017

Can't call callback with nodejs, aws lambda and aws api gateway

I am having trouble making the callback in lambda to send a response in API Gateway. I am trying to build a simple application that interacts with a MySQL database. Relevent code snippet is below.

The issue is that even though validateToken will return an error and I will see the console.log message the done/callback will never send the expected response to the api gateway and will continue to process any code after it.

exports.handler = (event, context, callback) => {

    const done = (err, res) => callback(null, {
        statusCode: err ? '400' : '200',
        body: err ? err.message : JSON.stringify(res),
        headers: {
            'Content-Type': 'application/json',
        },
    });

    if (event.path.match(/myApi\/workers\/*/)) {
      // Validate Token
      token.validateToken(pool, event.headers, function(err, result) {
        if (result.status == 'error') {
          console.log('Made it to here')
          done({"message": "Invalid api-token"}, null);
        } else {
          // Proceed with fetching workers
        }
      });
    }
  }

Any help would be greatly appreciated!



via davis190

No comments:

Post a Comment