javascript - "this" within prototype is undefined -
this question has answer here:
i have spent more hour on that.
what wrong 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 getting:
typeerror: cannot call method 'something' of undefined
why "studentservice" undefined ??
thanks lot!
your function isn't called in right context.
instead, try :
app.get('/students', studentcontroller.findall.bind(studentcontroller));
Comments
Post a Comment