I have a Controller with two functions, and one calls the other using this
, But when a call it the this
is undefined
I've tried to follow these instructions when I make an exemple it works, but when I implement it in my code the this stills undefined
.
Controller:
import AutenticacaoDAO from '../daos/autenticacao.dao'
import MailController from './email.controller'
import jwt from 'jsonwebtoken'
import TokenSecret from '../config/token-secret'
import crypto from 'crypto'
import { Request, Response } from 'express'
import { Usuario } from '../models/usuario.model';
class AutenticacaoController {
constructor() {
}
public async login(req: Request, res: Response) {
let usuario;
try {
usuario = await AutenticacaoDAO.login(req.body.email)
this.tratarResposta(await crypto.createHash("md5").update(req.body.senha).digest("hex"), usuario) //THIS IS UNDEFINED HERE
} catch (error) {
res.json(error)
}
}
tratarResposta (senhaEnviada: string, usuario: any) {
// HERE I TREAT IF THE RETURNED USER IS BLOQUED OR NAH, IF THE PASSWORD
//IS CORRECT AND SOME OTHER STUFF
}
}
export default new AutenticacaoController()
I'm able to do my stuff inside de try statement(it works, i've tried), but the login function will be too big...
route where I call the login method:
import { Router } from 'express'
import AutenticacaoController from '../controllers/autenticacao.controller'
const AutenticacaoRoutes = Router()
AutenticacaoRoutes.post('/autenticacao/signup', AutenticacaoController.signUp)
AutenticacaoRoutes.post('/autenticacao/login', AutenticacaoController.login)
AutenticacaoRoutes.put('/autenticacao/esquecisenha/', AutenticacaoController.forgotPassword)
AutenticacaoRoutes.get('/autenticacao/recoverypassword/', AutenticacaoController.recoveryPassword)
export default AutenticacaoRoutes