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

jason limmm
jason limmm
8,009 Points

running 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