actionhero / ah-newrelic-plugin

use newrelic within actionhero
2 stars 4 forks source link

Context of 'this' incorrect when adding middleware task. #6

Closed serenitygrant closed 7 years ago

serenitygrant commented 7 years ago
api.tasks.addMiddleware({
      name: 'NewRelic Task Middleware',
      global: true,
      priority: 1,
      preProcessor: (next) => {
        let worker = this.worker
        newrelic.createBackgroundTransaction(worker.job.class)
        next()
      },

      postProcessor: (next) => {
        newrelic.endTransaction()
        next()
      }
})

Running this causes the following error:

newrelic.createBackgroundTransaction(worker.job.class)
                                                   ^

TypeError: Cannot read property 'job' of undefined

However, changing preProcessor: (next) => to preProcessor: function(next) provides this with the correct context and actionhero starts without issue.

evantahler commented 7 years ago

Awesome. Please send in a pull request with this change!

serenitygrant commented 7 years ago

@evantahler Done! https://github.com/actionhero/ah-newrelic-plugin/pull/7