Skip to content

Instantly share code, notes, and snippets.

@itsMapleLeaf
Created October 15, 2017 00:37
Show Gist options
  • Save itsMapleLeaf/e079f77e462bf8a3a6a849dff1aec5c3 to your computer and use it in GitHub Desktop.
Save itsMapleLeaf/e079f77e462bf8a3a6a849dff1aec5c3 to your computer and use it in GitHub Desktop.
router.get(':/category', async (req, res) => {
try {
const category = await Category.findOne({ /* ... */ })
const posts = await Post.findAll({ /* ... */ })
res.render(/* ... */)
} catch (err) {
console.error(err)
}
})
@pixelprodev
Copy link

pixelprodev commented Oct 15, 2017

@kingdaro you can also do:

router.get('/:category', async (req, res, next) => {
  const category = await Category.findOne( /**/).catch(next)
  if (!category) { return res.sendStatus(404) }
  const posts = await Post.findAll(/**/).catch(next)
  res.json(/*stuff*/)
})

this assumes you have an error handler defined after your router(s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment