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

link submit a form leading an error

here is my js

router.get('/', (req, res)=>{
    res.render('ask');
});

router.post('/thankyou', (req, res)=>{
    res.cookie('name', req.body.name);
    res.render('thankyou');
});

my pug

    form#Name(action='/thankyou', method='post')
        label(for='name') Name:
        input#name(type='text', name='name' , placeholder='john doe')
        button(type='submit') Submit

i think i can post to a different route name just so long as i set the action of the form to the given post route name

i tried posting to the same route name by the way but it didn't work

1 Answer

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

What is the error you're getting when trying this?

When you tried posting to the same route name, did you have a POST route set up for it? Or only a GET?

Do you have a body-parsing middleware, like express.urlencoded() or express.json() implemented in your app to populate req.body?

What do you see if you try to console.log(req.body); before trying to use it to set cookies?

It's difficult to tell what could be going wrong without seeing the all of the code, hopefully these questions will spark an answer for you.

jason limmm
jason limmm
8,009 Points

i'll answer them one by one

  1. when i tried the same route, what i did was identical
router.get('/', (req, res)=>{
    res.render('ask');
});

router.post('/', (req, res)=>{
    res.cookie('name', req.body.name);
    res.render('thankyou');
});

i just removed the thankyou

  1. yes i do have middleware, body parser and cookie parser, i don't really know what else is there i need though

  2. it responded with this which i think confirms that 'name' is in the body [Object: null prototype] { name: '123' }

to all of these questions i answered yes but i still don't really have an idea but i think it can be from outside the two files i sent