'"status": 500, "message": "Error TypeError: Cannot read property 'catch' of undefined"

This question is resolved by Adding await before client.messages.create I am trying to send a WhatsApp message using Twilio. the following is the Code

const accountSid = '************************';
const authToken = '*************************';

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

    const client = require('rds/node_modules/twilio')(accountSid, authToken);
    try {
        console.log("Inside Try");
        client.messages
            .create({ 
                body: 'You are free to change it and write whatever you like.',
                from: 'whatsapp:**********',
                to: 'whatsapp:*********'
            }).then(message => {
                console.log("outbound SMS sent Successfuly :");
                console.log(message.sid);

                callback(null, {
                    statusCode: 200,
                    message: "Outbount message Sent Succesfuly :" + message.sid
                });
            }).done().catch(error => {
                console.log("Error occurred while sending outbound SMS," + error);
                 callback(null, {
                     status: 500,
                    message: "Error occurred while sending outbound SMS," + error
                });
            });
    
        
    }
    catch(error){
        //console.log("Error occurred while sending outbound SMS," + error);
        callback(null, {
            status: 500,
            message: "Error " + error
        });
    }
};

WhatsApp messages get sent successfully but I get this response as well

"status": 500,   "message": "Error TypeError: Cannot read property 'catch' of undefined"

and if I remove either done() or catch(error). I don't get any error message and it doesn't send wahtsapp message



Solution 1:[1]

You already mentioned the question that the done() invocation is the cause of this error. Your logic should be good if you remove it.

The other mentioned problem (that you don't receive the message) is probably due to the message payload that does neither match a message template nor belongs to an ongoing 24-hour session.

You should be able to find the detailed error message in the messaging logs in the Console. Note that errors from Meta/Whatsapp's servers will only appear there and they won't trigger a runtime error: enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 IObert