0

there are two classes ( I created new instance from first class in main class )

TypeError: Cannot read property 'first1' of undefined


export default class main {
  first1: first
  constructor() {
    this.first1 = new first()
  } 
  async list(req: Request, res: Response, next: NextFunction) {
    try {
      const { result } = await this.first1.list({ limit: req.query.limit, page: 
                         req.query.page, filters: req.query.filters })
      res.status(200).json(result)
    } catch (error) {
      console.log(error)
      CatchError(error, next)
    }
  }
}
export default class first{

  async list({ limit, page, filters }) {
    let result: {data:"ssssssss"}
    return { result }
  }

}
  • What's point of assigning value first to first1 property to object of main class when you are assigning value to the same property in constructor – ht006 May 16 '22 at 14:28
  • Please explain how you initialize your `main` instance? – Jérémie L May 16 '22 at 14:43
  • Please show us how you create a `new main` instance and how you call `.list()` on it. – Bergi May 16 '22 at 15:27
  • `export default class xyzRouter { router: Router private main1 = new main() constructor() { this.router = Router() this.routes() } private routes() { this.router.get("/", this.main1.list) } }` – Mohamed Abdelgaber May 16 '22 at 15:30
  • That's what I guessed: `this.main1.list` is not a method call, your passing an unbound function around. See the duplicate for how to fix this. Or maybe don't use `class` instances for everything, this wouldn't have happened with a simple `function` – Bergi May 16 '22 at 15:33

0 Answers0