Type error in Class method, [Ignore this and say the method does not exist]

Asked

Viewed 23 times

0

Good morning, I am facing a little problem, when calling via the express route a class method Controllercontact he says the method does not exist.

const nodemailer = require('nodemailer')
require('dotenv').config();

class ControllerContact{
    index(req,res){
        if(this.formatData(req.body.name,req.body.email,req.body.message)){
            res.json({code:200,text:'sucessfull'});
        }else{
            res.json({code:500,text:'Error'});
        }
    }

    formatData(name,email){
        this.sendMail(email,require('./views/thanks')({name,email})).catch(console.error);
        this.sendMail(email,require('./views/forMe')({name,email,message:'enviado'})).catch(console.error);
        return true;
    }

    async sendMail(mail,message){
        const transporter = nodemailer.createTransport({
            host: process.env.SMTP_HOST,
            port: process.env.SMTP_PORT,
            secure: process.SMTP_SECURITY,
            auth: {
              user: process.env.SMTP_USER,
              pass: process.env.SMTP_PASSWORD
            },
        });

        const info = await transporter.sendMail({
            from: '"'+process.env.SMTP_NAME+'" <[email protected]>',
            to: mail,
            subject: message[2],
            text: message[0],
            html: message[1],
        });
    }
}

module.exports = new ControllerContact();

The error that appears to me is as follows (Coming from the request):

TypeError: Cannot read property 'formatData' of undefined
    at index (/Users/erick/Desktop/portifolio/src/backend/src/contact/controller.js:6:17)
    at Layer.handle [as handle_request] (/Users/erick/Desktop/portifolio/src/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/erick/Desktop/portifolio/src/backend/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/erick/Desktop/portifolio/src/backend/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/erick/Desktop/portifolio/src/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/erick/Desktop/portifolio/src/backend/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/usuario/Desktop/portifolio/src/backend/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/usuario/Desktop/portifolio/src/backend/node_modules/express/lib/router/index.js:275:10)
    at expressInit (/Users/usuario/Desktop/portifolio/src/backend/node_modules/express/lib/middleware/init.js:40:5)
    at Layer.handle [as handle_request] (/Users/USUARIO/Desktop/portifolio/src/backend/node_modules/express/lib/router/layer.js:95:5)

I am using JS Common Non-typescript

  • 2

    I couldn’t reproduce that problem... For me (even in the browser console - removing unnecessary variables for minimal execution) the code even ran the method formatData.

  • 1

    I also could not reproduce the error.

1 answer

1

I assume your route is declared:

Route.post('controller', ControllerContact.index)

It turns out that when you pass the method index for the callback of the function Route he loses the context, soon this becomes Undefined.

Try to do this:

Route.post('controller', () => ControllerContact.index())

Note that the index continues to be coupled with the class in which it was declared

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.