Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$google undefined when using nuxt run build && npm run start #3

Open
zsalzbank opened this issue Mar 23, 2018 · 14 comments
Open

$google undefined when using nuxt run build && npm run start #3

zsalzbank opened this issue Mar 23, 2018 · 14 comments

Comments

@zsalzbank
Copy link
Contributor

When I run in development (npm run dev), everything works. When I build and serve the result, I get TypeError: Cannot read property 'maps' of undefined.

@WilliamDASILVA
Copy link
Owner

@zsalzbank Could you be more specific ?. Do you have some logs?
I can't reproduce it :/

@zsalzbank
Copy link
Contributor Author

The error I get in my browser console is:

TypeError: Cannot read property 'maps' of undefined
    at a.mounted (index.6688e3fbf0fc3cdc9711.js:2)
    at we (vendor.cc013dec96491fcb673b.js:2)
    at Object.insert (vendor.cc013dec96491fcb673b.js:2)
    at A (vendor.cc013dec96491fcb673b.js:2)
    at un.__patch__ (vendor.cc013dec96491fcb673b.js:2)
    at un.t._update (vendor.cc013dec96491fcb673b.js:2)
    at un.<anonymous> (vendor.cc013dec96491fcb673b.js:2)
    at Te.get (vendor.cc013dec96491fcb673b.js:2)
    at new Te (vendor.cc013dec96491fcb673b.js:2)
    at t (vendor.cc013dec96491fcb673b.js:2)

The code where I am trying to use this.$google is in a component (not in the pages directory):

mounted () {
  this.autocomplete = new this.$google.maps.places.Autocomplete(this.$refs.search, {
    types: [
      '(regions)'
    ],
    componentRestrictions: {
    country: 'US'
    }
  })
}

If I replace this.$google with window.google, everything works properly. If I keep the code as displayed above and run in development mode (npm run dev), everything works properly. If I keep the code as above and run npm run build && npm run start, then I get the error as indicated.

@WilliamDASILVA
Copy link
Owner

@zsalzbank Just tested and indeed, I get the error sometimes when the Google script is not loaded yet.
I'll take a more in-deep look to that. Can't you check if the $google object is defined before using it in the mean time?

mounted () {
   if (this.$google) {
      this.autocomplete = new this.$google....
   }
}

@zsalzbank
Copy link
Contributor Author

I could, but I still need that code to be called regardless (I'm using window.google for now as a workaround). Don't think I can watch this.$google to make sure it is defined - can I?

@sergioalvz
Copy link

It's happening the same for nuxt generate but, in this case, it does not even work with window.google instead of this.$google.

@WilliamDASILVA
Copy link
Owner

@zsalzbank @sergioalvz Alright, fixed the issue of $google being undefined in the creation hook for the v1.5.5. It injects an empty object, then remplace the property in the vue prototype with the getter referencing $google.

home.vue?4d24:62 Google from created in page:  {maps: {…}}
HomeHero.vue?1269:56 Google from created in component:  {maps: {…}}
HomeHero.vue?1269:59 Google from mounted in component:  {maps: {…}}
home.vue?4d24:65 Google from mounted in page:  {maps: {…}}

Could not reproduce the build / generate issue but it may fix it.
Could you confirm ?

@zsalzbank
Copy link
Contributor Author

Still having the same problem unfortunately.

@unkndono
Copy link

unkndono commented Jun 3, 2018

Hi there. 👍 Same problem for me, after npm run build & npm run start,

TypeError: Cannot read property 'maps' of undefined
this.autocomplete = new this.$google.maps.places.Autocomplete(this.input)

My module is configured like this:

['nuxt-google-maps-module', {
      defer: false,
      async: false,
      key: 'MyKey',
      libraries: [
          'places',
      ]
}],

@polidog
Copy link

polidog commented Jun 29, 2018

I had same problem.
But i solved use maps-module:loaded event.

mounted() {
  const execute = () => {
    // this.autocomplete = new this.$google....
  }
  
  if (this.$google) {
    execute();
  } else {
    const callback = () => {
      execute();
      window.removeEventListener('maps-module:loaded', callback)
    }
    window.addEventListener('maps-module:loaded', callback)
  }
}

@dimassrio
Copy link

Same here after generating, seems like the this.$google is not defined yet when it runs.

@david-j-davis
Copy link

Same issue here, thanks to @polidog, that fixed worked for me.

@antony
Copy link

antony commented Sep 27, 2018

I'm getting the same issue even with a regular npm run dev.

@rafaelmagalhaes
Copy link

doesn't seem to work in production only on dev environment

@tguelenman
Copy link

Had the same problem, @polidog workaround fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants