I am making an app in ruby on rails and this is one of my coffeescript files
I believe that my code is indented properly but I am still getting an error.
I marked the line that is giving me error with comment below.
Please help!
jQuery ->
today_date = new Date()
month = today_date.getMonth()
day = today_date.getDay()
pkpstyle= [
featureType: "landscape.natural"
elementType: "geometry"
stylers: [
lightness: -29
,
hue: "#ffee00"
,
saturation: 54
]
,
featureType: "poi.park"
stylers: [
lightness: -35
,
hue: "#005eff"
]
,
featureType: "road.arterial"
,
featureType: "road.arterial"
stylers: [ lightness: 45 ]
]
tempDay = 4
//I get an error here saying Uncaught TypeError: undefined is not a function
today_latlng = getLatlng(stops[tempDay])
markericon = new google.maps.MarkerImage("/assets/cycling.png")
myOptions =
center: today_latlng
zoom: 12
minZoom: 4
styles: pkpstyle
mapTypeId: google.maps.MapTypeId.ROADMAP
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)
for i of stops
latlng = getLatlng(stops[i].latlng)
marker = new google.maps.Marker(
map: map
icon: markericon
position: latlng
)
getLatlng = (loc) ->
loc_split = loc.split(", ")
lat = loc_split[0]
lng = loc_split[1]
new google.maps.LatLng(lat, lng)
This CoffeeScript:
Is, more or less, the same as this JavaScript:
So you do have a
getLatLngvariable when yougetLatLng(stops[tempDay])but you’re not assigning it a value until after you try to call it as a function. You need to definegetLatLngas a function before you treat it as one:Also, if
stopsis an array then you shouldn’t use anofloop, you should use aninloop:An
ofloop is the same as afor ... inJavaScript loop and that can do funny things with an array, aninloop ends up as afor(;;)loop in the JavaScript and that’s well behaved with arrays. I don’t know whatstopsis though so this might not apply, I’m just guessing based on how it is being used and theiindex variable.