Sunday, 14 May 2017

Trouble passing in my sql query into my cookie session

I am having trouble passing an object from my query into
my route. I am getting an error stating the object doesn't exist, and the promise doesn't resolve. What the heck!!!!

``
    getOnementee(mentee) {
        console.log(' this is the mentee from getOnementee, before the 
     query', mentee)
        const result = db.one('SELECT * FROM mentees WHERE username = $1', 
     [mentee.username]);
        console.log("this is the result: ", result)
        return result
      }

//So my SQL query has been made and I am exporting it. I've required the file, but the query fails to find mentee or create the cookie session

   app.get('/profile', sessionChecker, (req, res) => {
        console.log('checking in from profile!')
        let mentee = queries.getOnementee(req.body)
        mentee
        .then( mentee => {
        console.log('this is the value of mentee: ', mentee)
        res.render('profile', {mentee: mentee});
        })
   })

   app.use(session({
        key: 'user_sid',
        secret: 'somerandonstuffs',
        resave: false,
        saveUninitialized: false,
        cookie: {
            expires: 600000
        }
   }));

//This middleware will check if user's cookie is still saved in 
    browser 
   and user is not set, then automatically log the user out.
    // This usually happens when you stop your express server after 
   login, 
    your cookie still remains saved in the browser.
   app.use((req, res, next) => {
        if (req.cookies.user_sid && !req.session.user) {
            console.log('id check')
            res.clearCookie('user_sid');        
        }
        console.log('id else condition')
        next();
   });

    // middleware function to check for logged-in users
   var sessionChecker = (req, res, next) => {
        if (req.session.user && req.cookies.user_sid) {
            console.log('im in the if of session')
            res.redirect('/dashboard');
        } else {
            console.log("I'm in the else condition")
            next();
        }    
    };

``
As a sidenote, the above sessionschecker() hits the else condition; I 
don't think my cookies are being saved.



via LukeCage

No comments:

Post a Comment