Composition-api: fix: Cannot read property 'add' of undefined at onGlobalSetup

Created on 24 May 2021  路  7Comments  路  Source: nuxt-community/composition-api

馃悰 The bug
What isn't working? Describe what the bug is.
I found out after debugger, "const onGlobalSetup =(fn)=> {globalSetup.add(fn);};", in this code, globalSetup has not yet been assigned in the globalPlugin function

code show as below:

export default ({ app, store }, inject) => {
onGlobalSetup(() => {
inject('navigateTo', func)
})
}

bug

Most helpful comment

This should be fixed in 0.24.1 馃槉

All 7 comments

@JoeyYoung1997 Would you provide a reproduction? 馃檹

Working example.

I got the same error.
The following error is displayed on the browser console.

Chrome:

TypeError: Cannot read property 'add' of undefined
    at onGlobalSetup (index.mjs:459)

Firefox:

TypeError: globalSetup is undefined
    onGlobalSetup index.mjs:459

Same issue here,

client.js:102 TypeError: Cannot read property 'add' of undefined at onGlobalSetup (index.mjs:459)

While calling onGlobalSetup during a client plugin registration, seems that the variable globalSetup in the index.msj module has not been initialized:

let globalSetup;
const onGlobalSetup = (fn) => {
  globalSetup.add(fn);
};

Would you provide a reproduction so I can check this out?

Hi @danielroe,

Thank you for your prompt response, following is a reproduction:

https://codesandbox.io/s/goofy-night-t2sr6

The error occurs in plugin/someplugin.client.js

TypeError: Cannot read property 'add' of undefined

Calling globalPlugin before works for me as a workaround:

import { provide, onGlobalSetup, globalPlugin } from '@nuxtjs/composition-api'

const myPlugin: Plugin = function (context, inject) {
  globalPlugin(context, inject)
  onGlobalSetup(() => {
    provide(...)
  })
}

This should be fixed in 0.24.1 馃槉

Was this page helpful?
0 / 5 - 0 ratings