0

I have the following example:

import express from 'express';
class myController{
  constructor(){
    this.router = express.Router();
    this.router.get('/', this.getData);
  }
  
  getData(){
    console.log(this); //error
  }
}

What am i doing wrong?

  • 3
    did you try this.router.get('/', this.getData.bind(this)); ? as `getData` is used as a callback I think the context is not the one you expect when the callback is called, so you need to use the **bind** function to preserve the value of this – Olivier Boissé Aug 16 '16 at 13:34
  • See also: [How to access the correct `this` / context inside a callback?](http://stackoverflow.com/q/20279484/218196) – Felix Kling Aug 16 '16 at 14:54

0 Answers0