【问题标题】:Nuxt.js Server side plugin function is not a functionNuxt.js 服务器端插件函数不是函数
【发布时间】:2019-08-08 17:59:44
【问题描述】:

我创建了一个服务器端插件,我得到了

context.app.handleServerError 不是函数

// 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' },
  ],

//调用函数

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

我是否正确 asyncData 使呼叫服务器端? 根据docs,这个函数应该在上下文中可用。

【问题讨论】:

    标签: javascript vue.js nuxt.js


    【解决方案1】:

    在服务器端执行。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多