2

I'm doing an e-commerce site (learning purposes) with KeystoneJS. In the view where I display all the products I want to add a filter for sort the items by price and another one to show the products of only one brand. Two forms are needed but I don't get to submit only one

My products.pug looks like this

.container
  form(method='post')
    input(type='hidden', name='action', value='products')
    button(type='submit').btn.btn-primary Send

And my products.js in routes/views/ looks like this

[...]
// Print a word when submit the form
view.on('post', { action: 'products' }, function(next) {
  console.log('POST')
  next()
})
// Get all products from db
view.on('init'...)
// Render
view.render('products')

So basically what I want to do is to print POST when I hit the button in the view. Instead of that, I receive a 404 error page. I would really appreciate if you guys can help me

David Daza
  • 145
  • 8
  • I should add that if i change the method in the form and the verb in the event to GET and delete the second parameter it prints the word. Why doesn't it work with method post? – David Daza Jul 07 '17 at 22:46
  • This looks correct. Did you try making your form method `POST` instead of `post`? – Andy Hoffman Jul 07 '17 at 23:00
  • Yes, and keeps going wrong. I have chosen to use method GET and place all in only one form but I really want to solve this because, as you said, it seems right – David Daza Jul 08 '17 at 01:17

1 Answers1

1

Got it! In /routes/index.js I replaced

app.get('/products', route.views.products);

for

app.all('/products', route.views.products);

I feel dummy but happy.

David Daza
  • 145
  • 8
  • Finally I got it as well. Thanks to you. I had different input fields in one page and that might have caused the trouble. Any idea why we need this hidden input in the form `` ? – Juozas Rastenis Oct 07 '18 at 11:52
  • Glad I helped you! About the inputs: I guess this is only necessary if you are making different POST requests (multiple forms) and these are handled by the same route, so for every request you have a different `input:hidden` with a specific value and action. At least, it's what I do and I haven't had any issue so far. – David Daza Oct 17 '18 at 20:40