S
S
Sergey Beloventsev2016-08-31 22:00:31
Node.js
Sergey Beloventsev, 2016-08-31 22:00:31

Why does the Cannot set property 'user' of undefined error occur?

at /var/www/html/chatnode/routes/login.js:24:26
        at /var/www/html/chatnode/node_modules/async/dist/async.js:486:20
        at nextTask (/var/www/html/chatnode/node_modules/async/dist/async.js:5008:33)
        at /var/www/html/chatnode/node_modules/async/dist/async.js:5015:17
        at /var/www/html/chatnode/node_modules/async/dist/async.js:339:31
        at /var/www/html/chatnode/node_modules/async/dist/async.js:847:20
        at /var/www/html/chatnode/models/user.js:53:11
        at nextTask (/var/www/html/chatnode/node_modules/async/dist/async.js:5021:18)
        at /var/www/html/chatnode/node_modules/async/dist/async.js:5015:17
        at /var/www/html/chatnode/node_modules/async/dist/async.js:339:31
        at /var/www/html/chatnode/node_modules/async/dist/async.js:847:20
        at Query.<anonymous> (/var/www/html/chatnode/node_modules/mongoose/lib/model.js:3327:16)
        at /var/www/html/chatnode/node_modules/kareem/index.js:259:21
        at /var/www/html/chatnode/node_modules/kareem/index.js:127:16
        at nextTickCallbackWith0Args (node.js:419:9)
        at process._tickCallback (node.js:348:13)

here is routes/login.js
var User = require('models/user').User;
    var HttpError = require('error').HttpError;
    var AuthError = require('models/user').AuthError;
    var async = require('async');
    
    exports.get = function(req, res) {
      res.render('login');
    };
    
    exports.post = function(req, res, next) {
      var username = req.body.username;
      var password = req.body.password;
    
    
        User.authorize(username, password, function(err, user) {
            if (err) {
                if (err instanceof AuthError) {
                    return next(new HttpError(403, err.message));
                } else {
                    return next(err);
                }
            }
    
            req.session.user = User._id;
            res.send({});
    
        });
    
    };

swears at this line
req.session.user = User._id;
and at this section of code in models / user
schema.statics.authorize = function (username, password, callback) {
      
    
        var User = this;
          async.waterfall([
            function (callback) {
              User.findOne({username:username},callback);
            },
            function (user,callback) {
              if(user){
                if(user.checkPassword(password)){
                  callback(null, user);
                }else{
                  callback(new AuthError("Пароль неверен"));
                }
              }else{
                var user = new User({username:username,password:password})
                user.save(function (err) {
                  if(err)return callback(err);
                  callback(null, user);
                })
              }
            }
          ],callback);
        };
        exports.User = mongoose.model('User', schema);

swears at this part
if(user){
            if(user.checkPassword(password)){
              callback(null, user);
            }else{
              callback(new AuthError("Пароль неверен"));
            }

I’ll make a reservation right away, I’m learning from this screencast

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
samizdam, 2016-08-31
@Sergalas

Well, once
and
apparently, the session does not have such a property as user.
Yours, K.O.

D
Dmitry Belyaev, 2016-09-01
@bingo347

req.session - undefined
in undefined and null cannot be fields and, accordingly, an attempt to write or read such causes an error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question