routes.ts
import { Router } from 'express'
import { MyController } from './Controller/MyController'
const router = Router()
const myController = new MyController()
router.post('/doTheThing', myController.mainFunction)
export { router }
Then the controller
class myController {
async mainFunction(req: Request, res: Response) {
const data = req.body
if (data) {
const formattedData = formatData(data)
}
}
async formatData(data) {
// some code here
}
}
It gives the following error:
Cannot find name 'formatData' Did you mean the instance member 'this.formatData'?ts(2663)
Ok. It looks simple. Just add 'this' as the error mentions
The problem is if I add this.formatData(data)
I get a new error saying:
UnhandledPromiseRejectionWarning: TypeError: Cannot read formatData of undefined
I'm using Insomnia
to make the requests.
So what am I doing wrong? This should be something really simple. Calling a function from within the same class.