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 Pointsrunning into error with submitting forms
i get an error in my code after i submit the form in the /name route, and when i check my cookies in the chrome devtools it defines the name but name is undefined
here is all my code:
router.get('/name', (req, res)=>{
res.render('name');
});
router.post('/age', (req, res)=>{
const name = req.body.name;
res.cookie('name', name);
//i think there is something wrong below here
if(name){
res.render('age', {name});
} else{
res.redirect('/name');
};
});
block content
form(action='/ask/name', method='post')
label(for='name') Name:
input#name(type='text', name='name', placeholder='John Doe')
button(type='sumbit') sumbit
block content
h1 okay #{name}, what's your age?
form(action='/ask/age', method='post')
label(for='age')
input#age(type='text' name='age', placeholder="15")
button(type='sumbit') Submit
1 Answer
Travis Alstrand
Treehouse Project ReviewerHey jason limmm 👋
I didn't create an entire project to test these snippets you've provided of course, so maybe there is more that I'm not seeing, but from your post's question and the code I can see it appears...
The
/name
route does not have a POST route to take in that submitted form.The
/age
route does not have a GET route to show the user that second form.The
/age
POST route is trying to access thename
variable that would have been submitted on the first page's form.
I would either combine this into one form, if possible, or save the value of your name
field behind the scenes in that file once you make your POST route for /name
so that you can utilize it in the other route later.