I work in node.js. I'm trying to pass data from the form to the app:

app.get('/add', function(res, req, next) { var record = new Record({ text: req.body.login, login: req.session.login }); record.save(function(err, user, affected){ res.redirect('/main', { login: req.session.login }); }); }); 

This error pops up:

Cannot read property 'login' of undefined.

In the form and input field everything is spelled out, name, method, etc. The most interesting thing is that I do everything the same a little earlier, everything works. Perhaps this is due to app.use(express.bodyParser()); . Although through req.query.login also does not work.

  • The "login" property is not set. Why, I don't know, I have a bad relationship with Node.Js :) - user31688
  • The login property is not set, but the session property. - zb '
  • @eicto, I translated the error to him, but did not trace the error, because bad relationship with Node.Js :) - user31688
  • incorrectly translated Cannot read property 'login' of undefined who is undefined here? Do not login :) here see var req = {}; req.login; req.session.login; / * error is here, not higher * / - zb '21
  • @eicto, uh got to the bottom :) OK, so be it, although if you turn on the boring mode ... well, I don't want to. - user31688

2 answers 2

I will assume that you use express to create a web application. The latest version of express has been cleared and many built-in things have "migrated" to the plugins. For example, to enable session support, you need to install the express-session module, and then in the code

 var session = require('express-session'); ... app.use(session({secret: 'my-secret'})); 

After that you can use req.session in your routes.

https://github.com/expressjs/session

    Answer: the req and res variables are confused. Of course, the input parameters go first in the argument list. The correct option is:

     app.get('/add', function(req, res, next) { var record = new Record({ text: req.body.login, login: req.session.login });