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 trialjason limmm
8,009 Pointsage cookie not being defined
here is my code
router.post('/', (req, res)=>{
const name = req.body.Name;
res.render('survey', {name});
res.cookie('username', name);
});
router.post('/', (req, res)=>{
const age = req.body.Age;
res.render('survey', {age});
res.cookie('age', age);
});
extend layout
block content
form(action='/ask', method='post')
label(for='name') Name:
input#name(type='text', name='Name', placeholder='John Doe')
button(type='sumbit') sumbit
if name
h2 okay #{name}, how old are you
form(action='/ask', method='post')
label(for='age') Age:
input#age(type='text', name='Age', placeholder='13')
button(type='sumbit') sumbit
if i am doing this correct then the cookie "age" should put into the client and i can view it in the chrome dev tools but i don't know it's not defining though
1 Answer
Rohald van Merode
Treehouse StaffHey jason limmm 👋
You have two identical POST routes which will cause only the first one to ever execute. You'll want to adjust this to only use a single POST route 🙂
Hope this helps!
jason limmm
8,009 Pointsjason limmm
8,009 Pointsis something like this ok?
sorry i also forgot to add the pug
Rohald van Merode
Treehouse StaffRohald van Merode
Treehouse StaffNot quite jason limmm,
A
res.render()
call automatically ends the response cycle by sending the rendered HTML to the client. Any calls afterres.render()
will throw an error because the response has already been sent.You'll want to set any cookies, headers, or other response modifiers before calling
res.render()
to ensure they are properly included in the response.:Other than that you may want to have a look at your template. Currently you have two forms submitting to the same endpoint. This means that one or the other will always be undefined, you're either submitting the name, or the age value.
Hope this helps to get you going again!