0

I am writing a lambda function and returning a callback but the response is coming to be null.

My function looks like

var CloudmersiveValidateApiClient = require('cloudmersive-validate-api-client');
var defaultClient = CloudmersiveValidateApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'API-KEY';

// Create an instance
var apiInstance = new CloudmersiveValidateApiClient.EmailApi();

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

    var email = event.email;

    apiInstance.emailFullValidation(email, callbackcm);

    var callbackcm = function(error, data, responsed) {

        if (error) {
            callback(null, {
               "Error": JSON.stringify(error)
           });
        } else {
            callback(null, {
               "Body": JSON.stringify(data)
           });
        }
    };

};

Is there something wrong with the way i am returning?

3
  • 2
    Try to call apiInstance.emailFullValidation(email, callbackcm); after setting the callbackcm variable Commented Dec 17, 2018 at 8:05
  • @MaiKaY, Thanks for the input. It really helped. Commented Dec 17, 2018 at 9:57
  • @K.Liu, you can install it locally and then zip the entire code including the node_modules and upload it to lambda Commented Apr 20, 2020 at 12:38

1 Answer 1

1

Your sequencing is wrong. You assign the callbackm function after you've passed it in as an argument. You either need to do:

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

    var email = event.email;

    var callbackcm = function(error, data, responsed) {
        // ....
    };

    apiInstance.emailFullValidation(email, callbackcm);

};

or do this:

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

    var email = event.email;

    apiInstance.emailFullValidation(email, callbackcm);    

    function callbackcm (error, data, responsed) {
        // ....
    };
};

This is because javascript parses code in two phases. Google "hoisting" for more on how this behaves.

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

2 Comments

Also take a look at my answer to this other question for a more detailed exploration on corner cases: stackoverflow.com/questions/3887408/…
Thank you so much. I'll have a look at it as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.