Search code examples
javascriptnode.jspugkeystonejs

Event on Keystone JS view not working


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


Solution

  • 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.