4

Have a problem with using another method in class. I'm working with express framework and have to use Ecma Script 6. When I try to use method in the same class I have an error:

Cannot read property 'generatePassword' of undefined

TypeError: Cannot read property 'generatePassword' of undefined at createUser (E:\OpenServer\domains\testNode\app\controllers\users.js:5:13) at Layer.handle [as handle_request] (E:\OpenServer\domains\testNode\node_modules\express\lib\router\layer.js:95:5) at next (E:\OpenServer\domains\testNode\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (E:\OpenServer\domains\testNode\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (E:\OpenServer\domains\testNode\node_modules\express\lib\router\layer.js:95:5) at E:\OpenServer\domains\testNode\node_modules\express\lib\router\index.js:277:22 at Function.process_params (E:\OpenServer\domains\testNode\node_modules\express\lib\router\index.js:330:12) at next (E:\OpenServer\domains\testNode\node_modules\express\lib\router\index.js:271:10) at Function.handle (E:\OpenServer\domains\testNode\node_modules\express\lib\router\index.js:176:3) at router (E:\OpenServer\domains\testNode\node_modules\express\lib\router\index.js:46:12)

My code:

Route: /routes/users.js

const express = require('express');
const router = express.Router();
const users = require('../app/controllers/users');

router.get('/', users.createUser);

module.exports = router;

Controller: /app/controllers/users.js

class Users
{
    createUser(req, res)
    {
        this.generatePassword();
        res.json({msg: "The method return user data" });
    }

    generatePassword()
    {
        return 'The method return new password';
    }
}

module.exports = new Users();

Main App file: /app.js

const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');

const index = require('./routes/index');
const users = require('./routes/users');

const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  let err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

package.json

{
  "name": "testnode",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.15.2",
    "cookie-parser": "~1.4.3",
    "debug": "~2.2.0",
    "express": "~4.14.0",
    "hbs": "~4.0.1",
    "morgan": "~1.7.0",
    "serve-favicon": "~2.3.0"
  }
}

Help me please, thank you.

Ihor Byra
  • 107
  • 1
  • 1
  • 6

1 Answers1

4

In the line that causes the error, the object this is not what you think it is. To solve this, replace the following:

router.get('/', users.createUser);

with:

router.get('/', users.createUser.bind(users));

This will make sure when eventually that function is called by the router.get implementation, it will be called with the this object set to users.

Community
  • 1
  • 1
trincot
  • 211,288
  • 25
  • 175
  • 211