0

I have spent more than a hour on that.

What is wrong with this code?!

StudentController.js:

function StudentController() {
    this.studentService = {};
};

StudentController.prototype.findAll = function(req, res){
    this.studentService.something();
};

module.exports = StudentController;

app.js

var StudentController = require('./application/StudentController');
var studentController = new StudentController();
app.get('/students', studentController.findAll);

I am getting:

TypeError: Cannot call method 'something' of undefined

Why is "studentService" undefined ??

Thanks a lot!

AlonL
  • 6,100
  • 3
  • 33
  • 32

1 Answers1

1

Your function isn't called in the right context.

Instead, try :

app.get('/students', studentController.findAll.bind(studentController));
Yoann
  • 3,020
  • 1
  • 24
  • 34