AlexP2014
AlexP2014

Reputation: 211

passport.js - getting user ID to then query mongoDB

very new to passport.js so please forgive me.

Everything is working perfectly and I'm able to redirect the user once logged in and then display there username + a welcome message.

What I'm stuck with though is, how can I get the ID from req.user so I can then do a db.collections.find and display data from the database that has been inserted with that users ID? For example here is my get which redirects the user to index and shows there username

  app.get('/', function (req, res) {
      res.render('index', { user : req.user });
    console.log(req.user._id);

  });

But I can't get the .ID to show? / Am I even doing this in the right place? The console.log just crashed as ID is undefined, yet it is available in the req.user object when console.log'd?

Upvotes: 1

Views: 1086

Answers (1)

in3pi2
in3pi2

Reputation: 907

Try just putting req.user.id

app.get('/', function (req, res) {     
    console.log(req.user.id);
});

This will work once the user is logged into the system with passport

Upvotes: 1

Related Questions