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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -