1

I'm trying to verify otp using the sendotp npm module, but sendOTP.verify method i'm getting the result of callback as null. How can i verify whether otp has been verified of or not.

sendOTP - https://www.npmjs.com/package/sendotp

exports.handler = async (event,context,callback) => {
    const SendOtp = require('sendotp');
    const sendOTP = new SendOtp('260981AZN8sH3O5c551802');
    let mobileNum = event.mobile.toString();
    let otp = event.otp.toString();
    sendOTP.verify(mobileNum, otp, (error, data) => {
        callback(null,{statusCode:200,body:data.type});
    });
}
2
  • 2
    Have you tried to remove the async keyword? Commented Feb 2, 2019 at 20:18
  • 2
    Try removing the async keyword Commented Feb 2, 2019 at 20:18

1 Answer 1

3

There are two lambda function invocation types - synchronous and asynchronous. Synchronous invocation will block the caller which will wait for the response. Asynchronous invocation type will not block the caller and it will return immediately after it has been called with no - null - response.

If you place async keyword in front of your function, you are specifying the function to be called asynchronously, therefore the null response that you are getting is expected behavior.

If you don't want it to be asynchronous, then change your header to

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

Note that some services will use their predefined lambda invocation type when invoking lambda function without honoring your choice (i.e when you subscribe lambda function to s3 event, it will always be invoked asynchronously).

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

2 Comments

You are confusing handler functions declared with/without async ... and synchronous/asynchronous Lambda invocations. Despite the common use of the word "async," these two things are completely unrelated. Both coding styles are fully compatible with both invocation types. The problem here is that an async handler function needs to return a promise (or a value that will be implicitly promisifed), and not use callback.
thank you man, i totally forgot about that async keyword. It helped me. :D

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.