Error when rendering profile.pug (after user authentication)

In Advanced Node and Express, on

“Implementation of Social Authentication III”

Im passing all the tests - but when trying to login on the app myself I’m getting Error:

TypeError: Cannot read properties of undefined (reading ‘username’)
at /home/runner/boilerplate-advancednode/routes.js:26:49
at Layer.handle [as handle_request] (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/layer.js:95:5)
at next (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/route.js:137:13)
at ensureAuthenticated (/home/runner/boilerplate-advancednode/routes.js:84:14)
at Layer.handle [as handle_request] (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/layer.js:95:5)
at next (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/layer.js:95:5)
at /home/runner/boilerplate-advancednode/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/index.js:341:12)

So it seems something is wrong with username on line 26

app.route('/profile')
    .get(ensureAuthenticated, (res, req) => {
      res.render('profile', { username: req.user.username }) // (line 26)
    })

whole routes.js :

const passport = require('passport')
const bcrypt = require('bcrypt')

module.exports = function (app, myDataBase) {

  // response to render the Pug template
  app.route('/') // (app.route - chain multiple HTTP methods for a single route). also to define routes with middlewear
    .get((req, res) => {
      res.render('index', { 
        title: 'Connected to Database', // pug variable
        message: 'Please log in', // pug variable
        showLogin: true, // pug variable
        showRegistration: true,
        showSocialAuth: true
      })
    });

  app.route('/login')
    .post(passport.authenticate('local', { failureRedirect: '/' }), (req, res) => {
      res.redirect('/profile') // executes only if authenticate middlewear passes
    })

  app.route('/profile')
    .get(ensureAuthenticated, (res, req) => {
      res.render('profile', { username: req.user.username }) // 26
    })

  app.route('/logout')
    .get((req, res) => {
      req.logout();
      res.redirect('/');
  });

  app.route('/register')
    .post((req, res, next) => {
      myDataBase.findOne({ username: req.body.username }, (err, user) => {
        if (err) {
          next()
        } else if (user) {
          res.redirect('/')
        } else {
          const hash = bcrypt.hashSync(req.body.password, 12);
          myDataBase.insertOne({
            username: req.body.username,
            password: hash         
          }, (err, doc) => {
            if (err) {
              res.redirect('/')
            } else {
              next(null, doc.ops[0]); // inserted document is held within the ops property of the doc
            }
          })
        }
      })
    },
    passport.authenticate('local', { failureRedirect: '/' }),
    (req, res, next) => {
      res.redirect('/profile');
    }
  );

  app.route('/auth/github')
    .get(passport.authenticate('github'), (req, res) => {
      
    })

  app.route('/auth/github/callback')
    .get(passport.authenticate('github', { failureRedirect: '/' }), (req, res) => {
      res.redirect('/profile')
      
    })

  // 404
  app.use((req, res, next) => {
    res.status(404)
      .type('text')
      .send('Not Found');
  });

  // midleware for preventing not authenticated users to route
  function ensureAuthenticated(req, res, next) {
    if (req.isAuthenticated()) {
      return next();
    }
    res.redirect('/');
  };
  
}

But when deleting { username: req.user.username } from res.render - just to see the pug render (without the variable) - like this:

  app.route('/profile')
    .get(ensureAuthenticated, (res, req) => {
      res.render('profile')
    })

I’m getting another error :

TypeError: res.render is not a function
at /home/runner/boilerplate-advancednode/routes.js:26:11
at Layer.handle [as handle_request] (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/layer.js:95:5)
at next (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/route.js:137:13)
at ensureAuthenticated (/home/runner/boilerplate-advancednode/routes.js:84:14)
at Layer.handle [as handle_request] (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/layer.js:95:5)
at next (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/layer.js:95:5)
at /home/runner/boilerplate-advancednode/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/index.js:341:12)

Why cant the pug file render ?

solved:

i mixed up (res, req) - should be (req, res) :person_facepalming: