node.js - es6 class cannot read property of this -


here code

class modulecontroller {    constructor() {     this.testvar = 'xyz';   }    create(req, res, next) {     debug(this.testvar);   } } export default modulecontroller; 

i cannot read property 'create' of undefined why?

this node 7 babel , express -- latest version.

for have minimal knowledge es6 classes , use , come here kind of problem... others said in comments --- way calling class method faulty... did..

module = new modulecontroller();  const router = express.router();  router.route('/').post(module.create); 

which wrong... correct way is:-

module = new modulecontroller();  const router = express.router();  router.route('/').post((req, res, next) => module.create(req, res, next)); 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -