Ahmed Basha
Ahmed Basha

Reputation: 51

User.findOne() Always throw error Cannot read property '_id' of undefined - NodeJS

Here is my Middleware with Passport

passport.use('local-login', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
}, function (req, email, password, done) {
User.findOne({ email: email}, function (err, user) {
    if (err) return done(err);

    if(!user) {
        return done(null, false, req.flash('loginMessage', 'No user    found!'));
    }

    if (!user.comparePassword(password)) {
        return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
    }

    return done(null, user);
});
}));

and this is my login POST method

router.post('/login', passport.authenticate('local-login', {
    successRedirect: '/profile',
    failureRedirect: '/login',
    failureFlash: true
}));

After that, I'm trying to redirect to GET Profile page like that:

router.get('/profile', function (req, res, next) {
    User.findOne({'user._id': req.user._id}, function (err, user) {
        if (err) return next(err);

        res.render('accounts/profile', { user : user});
    });
});

Signup method woking correctly and send data to Database, but it's also has the error to redirecting to Profile page with the same TypeError !

Finally , here is dependencies in package.json

"dependencies": {
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.15.2",
"connect-mongo": "^1.3.2",
"cookie-parser": "^1.4.3",
"ejs": "^2.5.5",
"ejs-mate": "^2.3.0",
"express": "^4.14.0",
"express-flash": "0.0.2",
"express-session": "^1.14.2",
"mongoose": "^4.7.4",
"morgan": "^1.7.0",
"passport": "^0.3.2",
"passport-local": "^1.0.0"
}

I hope someone helps me, I'm stuck here! I can't move on.

Upvotes: 0

Views: 3062

Answers (3)

Ahmed Basha
Ahmed Basha

Reputation: 51

Finally I solved my error by just Downgrade some dependencies in my package.json file dependencies will be like so:

  "dependencies": {
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.14.2",
"connect-mongo": "^1.1.0",
"cookie-parser": "^1.4.0",
"ejs": "^2.3.4",
"ejs-mate": "^2.3.0",
"express": "^4.13.3",
"express-flash": "0.0.2",
"express-session": "^1.12.1",
"mongoose": "^4.3.4",
"morgan": "^1.6.1",
"passport": "^0.3.2",
"passport-local": "^1.0.0"
}

Then everything going correctly, at least in my case :) Thanks All.

Upvotes: 0

Bhavana
Bhavana

Reputation: 1054

From docs, to serialize and deserialize user to session use following code

passport.serializeUser(function(user, done) {
      done(null, user.id);
    });

passport.deserializeUser(function(id, done) {
  User.findById(id, function (err, user) {
    done(err, user);
  });
});

Upvotes: 1

sac Dahal
sac Dahal

Reputation: 1241

here you are trying to access user_id directly from the req object.

User.findOne({'user._id': req.user._id},

change it to

router.post('/profile', function (req, res, next) {
User.findOne({'user._id': req.body.user._id}, function (err, user) {
    if (err) return next(err);

    res.render('accounts/profile', { user : user});
});

});

You cannot send data in body of a GET request. So change it to a POST and then if you have not declared the bodyparser middleware then first declare it, then the data from your POST request will be available in your req.body.

Upvotes: 1

Related Questions