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 Express Basics Deeper into Routing with Express Statelessness, Setting and Reading Cookies

Rashid Mikati
Rashid Mikati
2,163 Points

I'm stuck :(

I am trying to get a name from the user, output "Welcome, name!" to the screen and then reload the page and still have the same welcome message with the same name on, just like what Andrew wants.

I am able to get the name, save it in a cookie and output the message. But when I reload the page the message disappears and it asks the user for a name again.

I have put a comment beside the line that is giving me an error. It is saying that "username" is undefined. Andrew put the exact same line of code. Can anyone figure out what's wrong with my code?

My express file:

const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const app = express();

app.use(bodyParser.urlencoded({extended: false})); //middleware that makes working with form data easier
app.use(cookieParser()); 

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

app.get('/' , (req, res) => {
    res.render("main", {question: "Who is Omar Ibn el Khattab?" , hint: "He is a muslim"});
});

app.get('/hello' , (req, res) => {
    res.render("hello" , {name: req.cookie.username}); //This is the line that is giving an error
});

app.post('/hello' , (req, res) => {
    res.cookie("username", req.body.username);
    res.render("hello" , {name: req.body.username});
});



app.listen(process.env.PORT , () => {
    console.log("app is running on port " + process.env.PORT);
});
Spencer Ailts
Spencer Ailts
15,302 Points

Hello Rashid,

It looks to me that you're missing an 's' in the line that is giving you an error.

try this - res.render("hello" , {name: req.cookies.username});

Rashid Mikati
Rashid Mikati
2,163 Points

Your a life saver Spencer! I found it very weird that the method on res is cookie and the method on req is cookies. It works after I added the s. Thanks a million!

Spencer Ailts
Spencer Ailts
15,302 Points

Glad I could help, I can't tell you how often I also miss just one letter.

1 Answer

Tom Geraghty
Tom Geraghty
24,174 Points

Pasting an Answer in from the comments so you can mark this question as resolved, Rashid.

Spencer Ailts on Jul 13 Hello Rashid,

It looks to me that you're missing an 's' in the line that is giving you an error.

try this

res.render("hello" , {name: req.cookies.username});