Issues on How to put a profile together on advanced node and express

Hey Guys I am stuck on that part of advanced node and express trying to request username in my profile page but always get the following error
“You should be passing the variable username with req.user.username into the render function of the profile page”
even though I have put the requested username inside the render function of the profile view page but still got the same error I have turned on free code camp some suggestions were to request the username from the body not from user but still has the same error
here is my full code
server.js

'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 ObjectID = require('mongodb').ObjectID;
const mongo = require('mongodb').MongoClient();
const LocalStrategy = require('passport-local');
const favicon = require('express-favicon'); 



const app = express();

fccTesting(app); //For FCC testing purposes
app.use(favicon(__dirname + 'https://cdn.gomix.com/2bdfb3f8-05ef-4035-a06e-2043962a3a13%2Ffavicon.ico')); 
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
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());

mongo.connect(process.env.MONGO_URI, (err, db) => {
  if(err) {
    console.log('Database error: '+err);
  
  
  } else {
  
  console.log('Successful database connection');
    
  passport.serializeUser((user, done) => {
  done(null, user._id);

});
    



 passport.deserializeUser( (id, done) => {

  db.collection('users').findOne(
    
      {_id: new ObjectID(id)},
      (err, doc) => {

        done(null, doc);
        

      }
  );
  
});
    passport.use(new LocalStrategy(
      function(username, password, done) {
      db.collection('users').findOne({ username: username}, function(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(user, null);
  });
  }
));
    

  app.route('/')
   .get((req, res) => {

    
    res.render(process.cwd() + '/views/pug/index', {title: 'Hello',message: 'Please login', showLogin: true});
  });
    app.route('/login')
    .post(passport.authenticate('local', {failureRedirect: '/'}),
     function(req, res) {
      
    
    res.redirect('/profile');
    
    });
    
    function ensureAuthenticated(req, res, next) {
      if(req.isAuthenticated()) {
        return next();
         
         
         }
      res.redirect('/')
    
    
    }
    
    
    app.route('/profile')
    .get(ensureAuthenticated, (req, res) => {
      
      res.render(process.cwd() + '/profile'+ { username: req.body.username });
    
    });

    app.listen(process.env.PORT || 3000, () => {
  console.log("Listening on port " + process.env.PORT);
  
});
    
}});


here is my profile.pug page

html
  head
    title FCC Advanced Node and Express
    meta(name='description', content='Profile')
    link#favicon(rel='icon', href='https://cdn.gomix.com/2bdfb3f8-05ef-4035-a06e-2043962a3a13%2Ffavicon.ico', type='image/x-icon')
    meta(charset='utf-8')
    meta(http-equiv='X-UA-Compatible', content='IE=edge')
    meta(name='viewport', content='width=device-width, initial-scale=1')
    link(rel='stylesheet', href='/public/style.css')
  body
    h1.border.center FCC Advanced Node and Express
    h2.center#welcome Welcome , {username}!
    a(href='/logout') Logout
    //add your code below, make sure its indented at this level
    
        
    script(src='https://code.jquery.com/jquery-2.2.1.min.js', integrity='sha256-gvQgAFzTH6trSrAWoH1iPo9Xc96QxSZ3feW6kem+O00=', crossorigin='anonymous')
    script(src='/public/client.js')

should be

res.render(process.cwd() + '/views/pug/profile',  { username: req.body.username });

The path is incorrect and the object is a function parameter not concatenated to the path string.

Thanks a lot I have passed this test :slight_smile:

This worked for me in the route, which was in the instructions.

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