3

Im trying to build a simple login system but when i try the asign the session i get this error

Cannot set property 'user' of undefined

However this is my code:

app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,}))

This is the login controller:

    const loginModel = require('../model/loginModel');
    module.exports = {
    login : async function(req,res){
        try {
        const query = await loginModel.login(req.body.email,req.body.password);
        if(query != null){
            req.session.user = query;
            res.status(200).json(req.session.user);
        }else{
            res.status(401).json({error : true});
        }
        } catch (error) {
            console.error(error.message);
            res.status(500).send('something went wrong');
        }
        
    }
}

NOTE : login model is 100% working and its retrieving the user from database

5
  • 1
    Are you running the express-session middleware before this request? That is required to create the req.session object and to tie it to a cookie. Commented May 5, 2021 at 22:48
  • you mean running the first code ? yes im running the first code with nodemon Commented May 5, 2021 at 22:50
  • 1
    Well, something is not working properly with the session middleware because req.session does not appear to exist based on the error you are getting. Are the routes declared in the right order (with the session middleware first). Perhaps you should show us much more of your code so we can see where these two things fit relative to one another. It's also possible that you're passing the wrong arguments to the login() function. We can't see what the problem is without seeing more code. Commented May 5, 2021 at 22:52
  • okay i'll add more core Commented May 5, 2021 at 22:54
  • i found the error i called the middleware in the wrong file xD i should called it in index.js .Thank you my friend Commented May 5, 2021 at 22:59

1 Answer 1

1

Well, something is not working properly with the session middleware because req.session does not appear to exist based on the error you are getting. Here are some of the things it could be:

  1. Your session middleware is not getting properly run.

  2. Your session middleware is running AFTER the route in which you're trying to use req.session. It must be run BEFORE because that middleware is what creates the req.session object and loads its data based on the session cookie.

  3. Based on this code, you could also get this error if you were passing the wrong req argument to your login(req, res) method so it's worth checking that too.

We can't see specifically what the problem is without seeing more code.

Sign up to request clarification or add additional context in comments.

1 Comment

session middleware wasn't running properly .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.