0
router.get('/profilePage/:userid', function(req, res) {
    var userid = req.session.user
     console.log(userid)
    if (!userid) {
     //No user id provided
     } else {
    pool.query('SELECT * from users where id = ?',
    [userid], 
   (error, results, feilds)=> {
        if (error) {
            console.log("error ocurred while getting user details of " + userid, error);
            res.send({
                "code": 400,
                "failed": "error ocurred"
            });
        } else {
            if (results.length > 0) {
            const profile = results[0]
            //You can then access the user id with
            console.log(profile.fullname)
            
            res.render("profilePage",{profile});
            } else {
             console.log('unable to retrieve a profile')
            }
        }
    });
}
});

how can I make the userid = logged-in user? I tried many ways such as sessions (var userid = req.session.user) but all did not work what is the correct way to make the userid = to logged-in user id?

Yunes Argash
  • 13
  • 1
  • 3
  • Well, how do you determine whether someone is logged in anywhere else in your app? What does logging in do? – Kelvin Schoofs Jul 26 '21 at 22:01
  • i am using this exact code, and it is working for me req.session.user = user; – Yunes Argash Jul 26 '21 at 22:04
  • It's a bit weird. You have a route `/profilePage/:userid` but instead of using your route parameter (`req.params.userid`) you're using the id of the logged in user? – Kelvin Schoofs Jul 26 '21 at 22:05
  • if I will use (req.params.userid) I have to pass the id of the user in the browser for example http://localhost:3002/profilePage/76 but what i want it to be is auto generating of the id – Yunes Argash Jul 26 '21 at 22:09
  • Auto generating? You mean that `/profilePage` leads to your own profile? According to [this answer](https://stackoverflow.com/a/6784955/14274597) you can make that parameter optional, then use `req.params.userid || req.session.user`. Otherwise, add a new `/profilePage` route (without the parameter) that just uses the session user. – Kelvin Schoofs Jul 26 '21 at 22:14
  • You mean that /profilePage leads to your own profile? Yes – Yunes Argash Jul 26 '21 at 22:18

0 Answers0