BamBam22
BamBam22

Reputation: 713

Nuxt.js Server side plugin function is not a function

I created a server side plugin and I'm getting

context.app.handleServerError is not a function

// hanlde-server-error.js

export default ({ app }, inject) => {
  app.handleServerError = (method, error, data) => {
    const message = `An error occured in ${method}. ${error}`
    console.error(message)
    Sentry.captureException(new Error(message))
  }
}

// nuxt.config.js

  plugins: [
    { src: '~plugins/handle-server-error', mode: 'server' },
  ],

// calling function

  async asyncData(context) {
    // await store.dispatch('fetchAccounts')
    try {
      await undefinedFunction()
    } catch (error) {
      context.app.handleServerError('asyncData', error, { user: 'bambam' })
    }
  },

Am I correct that asyncData makes the call server side? According to the docs this function should be available on context.

Upvotes: 0

Views: 4171

Answers (1)

Andrew Vasylchuk
Andrew Vasylchuk

Reputation: 4779

Execute it only server side.

async asyncData(context) {
  if (process.server) {
    try {
      await undefinedFunction()
    } catch (error) {
      context.app.handleServerError('asyncData', error, { user: 'bambam' })
    }
  }
},

Upvotes: 1

Related Questions