Stuck on advanced Node challenge: How to Put a Profile Together Sort by

I’m quite stuck on a particular challenge with advanced Node. The directions for this challenge are not very clear, but here is the part I am struggling with:

“Go ahead and pass the object containing the variable username equaling ‘req.user.username’ into the render method of the profile view.”

I believe I have done this toward the bottom of my code in the block that begins app.route('/profile'), but I continue to fail the tests no matter how I reorganize the code.

Here is a link to the challenge, and here is my code so far.

'use strict';

const express     = require('express');
const bodyParser  = require('body-parser');
const fccTesting  = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session')
const passport = require('passport')
const mongo = require('mongodb').MongoClient
const ObjectID = require('mongodb').ObjectID
const LocalStrategy = require('passport-local')

const app = express();

app.set('view engine', 'pug')

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
}))

app.use(passport.initialize())
app.use(passport.session())

function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {
    return next()
  }
  res.redirect('/')
}

// connect with database, serialize user info, connect to port 3000
mongo.connect(process.env.DATABASE, (err, db) => {
  if (err) {
    console.log('Database error: ' + err)
  } else {
    console.log('Successful database connection')
    
    passport.serializeUser((user, done) => {
      done(null, user._id)
    })
    
    app.listen(process.env.PORT || 3000, () => {
      console.log("Listening on port " + process.env.PORT);
    })
    
    // deserialize user info
    passport.deserializeUser((id, done) => {
      db.collection('users').findOne(
        {_id: new ObjectID(id)},
        (err, doc) => {
          done(null, doc)
        }
      )
    })
    
    // check local database for username and password authentication
    passport.use(new LocalStrategy(
      (username, password, done) => {
        db.collection('users').findOne({username: username}, (err, user) => {
          console.log('User ' + username + ' attempted to log in.')
          if (err) {return done(err)}
          if (!user) {return done(null, false)}
          if (password !== user.password){return done(null, false)}
          return done(null, user)
        })
      }
    ))
    
    app.route('/')
      .get((req, res) => {
        res.render(process.cwd() + '/views/pug/index.pug', {title: 'Home page', message: 'Please login', showLogin: true});
      });

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

    app.route('/profile')
      .get(ensureAuthenticated, (req, res) => {
        const username = req.user.username
        res.render(process.cwd() + '/views/pug/profile' + username)
      })
  }
})

fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

Any help would be appreciated.

This is odd. Aren’t you supposed to pass the object like you did some lines above it?

// looks weird
res.render(process.cwd() + '/views/pug/profile' + username)
// looks right
res.render(process.cwd() + '/views/pug/index.pug', {title: 'Home page', message: 'Please login', showLogin: true});
1 Like

You got it. I got a bit turned around.