MickeyTheMouse
MickeyTheMouse

Reputation: 419

POST request from handlebars form gives 404

I have the following form in my handlebars code:

{{#if contact}}
    <form method="POST" action="/contacts/{{contact._id}}">
        <input type="text" name="name">
        <input type="text" name="phone">
        <button>Update Contact</button>
    </form>
{{else}}

When the user clicks the button, my browser is redirected to 'localhost:3000/contacts/34634234'

and I get a 404 error.

I checked, and 34634234 is a valid ID.

In my Node routes, I have

router.post('contacts/:id', function(req, res) {
  res.render('index');
});

I know res.render('index') works because I've used it in other parts of my code.

However,

Upvotes: 1

Views: 968

Answers (1)

Diogo Capela
Diogo Capela

Reputation: 6570

Not sure if is because of this, but...

You're missing type="submit" on the button element.

{{#if contact}}
    <form method="POST" action="/contacts/{{contact._id}}">
        <input type="text" name="name">
        <input type="text" name="phone">
        <button type="submit">Update Contact</button>
    </form>
{{else}}

And you're missing a slash / at the beginning of your route.

router.post('/contacts/:id', function(req, res) {
  res.render('index');
});

Upvotes: 2

Related Questions