The pre and post middleware not run as expected


#1

Hi everyone, I need a help.

I have a route like this.

Route.post('/kosts/:kostId/tambah-gambar', 'KostController.tambahGambar')
  .middleware(['auth', 'auth2', `role:${consts.USER_LEVEL_OWNER}`, 'findKost', 'checkKostOwner', 'findKostAfter', 'filterKomentar'])

I’m using some middlewares, some run before the controller runs, some run after the controller runs (findKostAfter & filterKomentar run after the controller runs). Without those two middlewares, the route return expected response.

But with those two middewares, the route returns unexpected response which is located in FindKost middleware in the catch block.
Here is FindKost middleware.

class FindKost {
  async handle({ params: { kostId }, request, response }, next) {
    try {
      const kost = await Kost
        .query()
        .with('pemilik')
        .with('gambars')
        .with('favorits.sender')
        .with('komentars.sender')
        .with('variabels.satuan')
        .where({ id: kostId })
        .first()

      if (kost) {
        if (kost.pemilik.blocked === 1) {
          response
            .status(404)
            .json({
              message: 'Kost not found',
              kostId
            })
        }

        request.body.kost = kost
        console.log('FindKost 1', kost.id) // this log printed
        await next()

      } else {
        response
          .status(404)
          .json({
            message: 'Kost not found',
            kostId
          })
      }
    } catch (error) {
      console.log('FindKost 2') // I don't expect this log is printed
      response
        .status(404)
        .json({
          message: 'Kost not found oi',
          error,
          kostId
        })
    }
  }
}

Here is CheckKostOwner middleware

class CheckKostOwner {
  async handle({ request, response }, next, args) {
    const { user, kost } = request.post()
    // console.log(user.level, args);

    if ((user.level === consts.USER_LEVEL_OWNER && kost.user_id === user.id) ||
      args.indexOf(user.level) >= 0) {
      console.log('CheckKostOwner')
      await next()

    } else {
      response
        .status(401)
        .json({
          message: 'Authorization failed'
        })
    }
  }
}

Here are those two middleware which run after the controller run, FindKostAfter and FilterKomentar. It breaks on FindKostAfter middleware and doesn’t reach the FilterKomentar middleware.

class FindKostAfter {
  async handle({ request, response }, next) {

    console.log('FindKostAfter 1') // only this line 
    await next() // here, it should call the controller
    console.log('FindKostAfter 2') // this line log not printed
  }
}

Here is the controller

async tambahGambar({ request, response }) {
  console.log('KostController 5') // this log printed
 
  response
    .status(200)
    .json({
      message: 'Gambar sukses ditambah',
      newGambar,
      kost,
      movedGambar
    })
}

This is the log printed in my terminal and the response on Postman
Screenshot%20from%202018-10-10%2020-18-10
Screenshot%20from%202018-10-10%2020-18-48

Hopefully your help, thank you


#2

Anyone can help me please?