Answer the question
In order to leave comments, you need to log in
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)
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({});
});
};
req.session.user = User._id;
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);
if(user){
if(user.checkPassword(password)){
callback(null, user);
}else{
callback(new AuthError("Пароль неверен"));
}
Answer the question
In order to leave comments, you need to log in
Well, once
and
apparently, the session does not have such a property as user.
Yours, K.O.
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 questionAsk a Question
731 491 924 answers to any question