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 Solution: Clearing Cookies and Redirecting

Howard Goldstein
Howard Goldstein
9,399 Points

What is the value of a cookie after using res.clearCookies?

I struggled with this challenge because even when using the answer from the course I couldn't create a boolean statement that would recognize when a cookie had been cleared.

After much trial and error it seems that after clearing cookies the value of the cookie was becoming a string with value 'undefined'. So eventually I was able to get it work using the following...

app.get('/hello', (request, response) => { const name = request.cookies.username;

if (name && name != 'undefined') response.redirect('/'); else response.render('hello'); });

Any idea what I might be doing or misunderstanding or what might be different that is leading to me having this issue - but the course / others aren't?

Trent Stenoien
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trent Stenoien
Full Stack JavaScript Techdegree Graduate 21,632 Points

Are you sure you should be using app.get() instead of router.get()? Where ever you assigned your router is what you should be using. After that 'if(name){}' should be enough since a value of undefined will return false.

const express = require('express');
const router = express.router();

app.get('/hello', (req,res) => {
    const name = req.cookies.username;
    if (name) {
        //The rest
    }

1 Answer

Howard Goldstein
Howard Goldstein
9,399 Points

Thanks for the quick reply. I'm using the following to setup the app object, which seems to work and seems to be consistent with how the course does it.

const express = require("express");
const app = express();

That being said, I just figured out my error... I was never actually calling response.clearCookie! I made an error in what was redirected what where such that it replicated the exact behavior you would expect if it was done correctly but just without the actual response.clearCookie. As part of the error, the program ended up assigning the cookie a value of an object that was undefined which it then interpreted as a string with value "undefined". So it looked undefined to me, but was actually a string with the value undefined. Long story... but basically I was making a mistake. At least I learned how to properly post code into the comments.

Thanks.