eawer
eawer

Reputation: 1458

Why geolocation data in not returning from the helper to the template

Here is my Meteor code snippet - http://meteorpad.com/pad/sYoY8Xzjzd3sejFC4/Location

As you can see, in should write your current location in the <p> tag (you can check if res[0].formatted_address has any data by opening browser console - it should contain your current location).

But for some reason <p> remains empty.

Problems begin in the navigator.geolocation.getCurrentPosition callback - if I modify the code, so the data is returned outside the callback - data goes to the <p>

Example:

Template.location.helpers
  location: ->
    navigator.geolocation.getCurrentPosition (position) ->
      if GoogleMaps.loaded()
        geocoder = new google.maps.Geocoder()
        geocoder.geocode {'latLng': new google.maps.LatLng(position.coords.latitude, position.coords.longitude)}, (res, status) ->
          console.log res[0].formatted_address
          res[0].formatted_address
    return 123

I tried to play with Session variables, and it worked - http://meteorpad.com/pad/nR4vgj7HqJvCdt3wE/Location-Session, but why information from the callback is not going to the template, and how to fix it?

Upvotes: 0

Views: 80

Answers (1)

carloss medranoo
carloss medranoo

Reputation: 1489

Thats because the res[0].formatted_address is not reactive data like a Session variable.

and at the time the helpers return res[0].formatted_address to the template the value is ''.

You can check it like this.

var test = res[0].formatted_address
 if test != ''
  console.log 'hi iam a non reactive value and also im returning nothing'
 else
   console.log "yea my response is " + res[0].formatted_address

And ofc you will get the first console.log because at the moment you return the value to the template the value is ''

The Session variable works, since the Session is a reactive data, and also the helper run assyncronus, so first the helper look for the Session.get('location') wich value is '' but some seconds leater when the callback on the geolocation finish the value of the session is diferrent and the computation of the helper reruns the function and it return the value.

Upvotes: 1

Related Questions