15

This is the code running in an aws lambda function.

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

          mqfunc1(func2);



};
var func2 = function(data) {
            console.log('got data: '+data);

};
var mqfunc1 = function(callback) {
        var myCallback = function(data) {
        console.log('got data: '+data);
        };

        var usingItNow = function(callback) {
        callback('get it?');
        };

};

Now I do get the the message which i want to print in the console. But I want to show the same message printed in the console using the callback function inside exports.handler.

I tried using callback function using various ways inside exports.handler but I am always getting null. I do understand that in a node js scripts all the function calls are asynchronous, so how do I return a value from any function and callback the same inside exports.handler, i.e. display the same in Execution result.

1
  • Can you add a sample output of what you are expecting. Commented Dec 30, 2016 at 9:34

2 Answers 2

26

That was the old version of lambda i.e for Node.js runtime v0.10.42. The new AWS version callback has two arguments i.e for Node.js runtime v4.3 or v6.10

callback(response_error, response_success)

module.exports.publisher = (event, context, callback) => 
{
   callback(response_error, response_success)
}

I tend to pass a status code, in case I want to use the result after my job is done:

const response_success = {
      statusCode: 200,
      body: JSON.stringify({
        message: 'ok'
      }),
};

const response_error = {
    statusCode: 400,
    body: JSON.stringify({
        message: 'error'
    }),
};

if (error) {
   callback(response_error)
} else {
   callback(undefined, response_success)
}

Reference: http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

Sign up to request clarification or add additional context in comments.

3 Comments

I think you have a typo in your second last line of the code sample. You want to pass response_success to the callback as second parameter instead of response_error
I'm getting---->>> { "message": "Internal server error" } It should be showing my custom message
This is the closest I've gotten so far to this working... But I would like to mention just for future readers: The behavior of this callback function is VERY finicky and often doesn't work at all. Idk why but AWS seems to have done a very bad job at this and it oftentimes gives an internal server error or returns an empty message.
6

You should call the callback function itself — the one passed as an argument to exports.handler.

E.g.:

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

var mqfunc1 = function(callback) {
    callback({'result': 'success'});
};

2 Comments

Thanks, it was really helpful for a newbie like me :)
bear in mind that the callback function's first argument is for errors, and second is for success

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.