简体   繁体   中英

coffescript for the Uncaught TypeError: Cannot read property 'offsetWidth' of undefined error

I'm using the gem gmaps4rails and getting the error when I check the developer tools.

Uncaught TypeError: Cannot read property 'offsetWidth' of undefined  

I have read over the other solutions on here and Know that the problem is how the script is loading before the window.

here is the initializer code (stores.js.coffee)

gm_init = ->
  gm_center = new google.maps.LatLng(53,6)
  gm_map_type = google.maps.MapTypeId.ROADMAP
  map_options = { center: gm_center, zoom: 8, mapTypeId: gm_map_type }
  new google.maps.Map(@map_canvas, map_options)

map = gm_init()

I have taken some of the tips and adviece that the other questions have given me but have been unable to fix the code, I think this line should fix the code.

window.onload = ->

I have placed this line, and others in various places in my code but I have had no success. I'm new to ruby on rails and coffeescript, I know the solution is relatively simple. I'm just having difficulty correctly implementing this fix.

Any help is very much appreciated.

Actually I understand the issue: you're not giving your map a place to live.

I'd bet @map_canvas is undefined.

(why dont you use gmaps4rails if you already have the gem installed?)

Try: window.onload = (-> map = g.init() )

If you still get the error try and find what object is undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM