Welcome to the Linux Foundation Forum!

ch5 throw notFound() is not defined

  1. fastify.get('/:id', async (request, reply) => {
  2. const { id } = request.params
  3. try {
  4. return await read(id)
  5. } catch (err) {
  6. if (err.message === 'not found') throw notFound()
  7. throw err
  8. }
  9. })

Does not work anymore. it return that err

  1. "err": {
  2. "type": "ReferenceError",
  3. "message": "notFound is not defined",
  4.  

I found this fix on the web: https://github.com/fastify/fastify/issues/402

  1. const { NotFound } = require('http-errors')
  2.  
  3. module.exports = async(fastify, opts) => {
  4. fastify.get('/:id', async(request, reply) => {
  5. const { id } = request.params
  6. try{
  7. const d = await promisify(bicycle.read)(id)
  8. reply.send(d)
  9. } catch(e) {
  10. console.log('the errrrrrrrr: ', e.message)
  11. if(e.message === 'not found') throw new NotFound()
  12. // if(e.message === 'not found') throw notFound()
  13. else throw e
  14. }
  15. })
  16. }

Comments

  • hi @djedje the material talks about using fastify-sensible - this supplies the http-errors notFound method on the reply object. If you skipped the fastify-sensible step (or otherwise removed it, since the new create-fastify project now includes it, then you would hit the notFound is not defined error.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training