Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript User Authentication With Express and Mongo Improving the App with Custom Middleware Using Session Variables to Customize Content

Why am I getting this error when I run localhost?

I am getting this error when I run localhost. I checked my code and can't find the problem. Any help on resolving this is greatly appreciated. Here is the error:

Error: C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\views\navbar.pug:17:81
    15|         if currentUser
    16|             div
  > 17|                 img.avatar.img-circle.hidden-xs-down(src='/images/avatar.png', alt='avatar')        
----------------------------------------------------------------------------------------^
    18|                 a.btn.btn-light.pull-md-right(href='/logout') Log out
    19|             else
    20|                 a.btn.btn-info.pull-md-right(href='login') Login

unexpected text "       
        "
    at makeError (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-error\index.js:32:13)
    at Lexer.error (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-lexer\index.js:58:15)
    at Lexer.fail (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-lexer\index.js:1301:10)
    at Lexer.advance (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-lexer\index.js:1361:15)
    at Lexer.callLexerFunction (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-lexer\index.js:1316:23)
    at Lexer.getTokens (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-lexer\index.js:1372:12)
    at lex (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-lexer\index.js:12:42)
    at Object.lex (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug\lib\index.js:99:27)
    at Function.loadString [as string] (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-load\index.js:44:24)
    at C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-load\index.js:31:27
    at walkAST (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-walk\index.js:23:18)
    at C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-walk\index.js:104:20
    at Array.reduce (native)
    at walkAndMergeNodes (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-walk\index.js:103:18)
    at walkAST (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-walk\index.js:37:19)
    at walkAST (C:\Users\Rusie\Documents\susan_treehouse\User_Authentication_with_Express_and_Mongo\Using_Session_Variables_to_Customize_Content\S3V5\node_modules\pug-walk\index

Here is my code:

nav.navbar.navbar-fixed-top.navbar-dark.bg-inverse
        button.navbar-toggler.hidden-md-up.pull-xs-right(type='button', data-toggle='collapse', data-target='#navbar') ☰
        #navbar.navbar-nav.collapse.navbar-toggleable-sm
            .container
                a.navbar-brand(href='/')
                    i.icn-logo.material-icons bookmark_border
                    | Bookworm
        .nav-items.clearfix
            if !currentUser
                a.nav-item.nav-link(href='register') Sign Up
        a.nav-item.nav-link(href='about') About
        a.nav-item.nav-link(href='contact') Contact
        a.nav-item.nav-link(href='profile') My Profile

        if currentUser
            div
                img.avatar.img-circle.hidden-xs-down(src='/images/avatar.png', alt='avatar')        
                a.btn.btn-light.pull-md-right(href='/logout') Log out
            else
                a.btn.btn-info.pull-md-right(href='login') Login
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var session = require('express-session');
var app = express();

// use sessions for tracking logins
app.use(session({
  secret: 'treehouse loves you',
  resave: true,
  saveUninitialized: false
}));

// make user ID available in templates
app.use(function (req, res, next) {
    res.locals.currentUser = req.session.userID;
    next();
});

// mongodb connection
mongoose.connect("mongodb://localhost:27017/bookworm");
var db = mongoose.connection;
// mongo error
db.on('error', console.error.bind(console, 'connection error:'));

// parse incoming requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// serve static files from /public
app.use(express.static(__dirname + '/public'));

// view engine setup
app.set('view engine', 'pug');
app.set('views', __dirname + '/views');

// include routes
var routes = require('./routes/index');
app.use('/', routes);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('File Not Found');
  err.status = 404;
  next(err);
});

// error handler
// define as the last app.use callback
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });

Thanks in advance.

4 Answers

Angela Visnesky
Angela Visnesky
20,927 Points

Hi Susan! Have you tried removing the comma between you src and alt on line 17? Hope this helps!

I tried that and still got the same error. I even went back into my app.js and found a typo with a "-" instead of an "=" and it still gave me the same error. I am perplexed. I wonder if it has to do with Pug itself. I know it can give you problems if you do a combination of tabs and spaces using the spacebar. It only wants you to do one or the other. If you have any other ideas what to try, I am all ears.

Hi Susan,

It looks like your if-else statement is indented incorrectly. Unindent the else and maybe that's what's wrong.

Adam Beer
Adam Beer
11,314 Points

Hi Susan. I hope not too late. In your app.js check and fixed the sixteenth row. You misspelled. res.locals.currentUser = req.session.userID; The last letter isnt big D, it is a small d :) Enjoy your perfect code!

Adam Beer
Adam Beer
11,314 Points

And please correct the logical code below the if !currentUser