Call this.push in Polymer({ }) function from HTML5's geolocation callback

33 views
Skip to first unread message

Abhilash Sathe

unread,
Nov 22, 2016, 10:43:29 AM11/22/16
to Polymer
navigator.geolocation.getCurrentPosition(function(position) {
lat = position.coords.latitude,
lng = position.coords.longitude;
this.push('thread.messages', {
    latlong: lat+","+lng,
});

}, function() {
console.warn('ERROR(' + err.code + '): ' + err.message);
}); In the above, I get an error saying `push` is not a function. How should I handle this?

Karl Tiedt

unread,
Nov 22, 2016, 11:05:38 AM11/22/16
to Abhilash Sathe, Polymer
Basic Javascript 101 -- you are inside of a async callback which has no scope to your original method.

In ES6 style...
navigator.geolocation.getCurrentPosition((position) => { /*code here */ });

In Non ES6... (by using a bound method)
navigator.geolocation.getCurrentPosition(this.updateCoords);

Non ES6 v2... (via bind)
navigator.geolocation.getCurrentPosition(function(position) {
lat = position.coords.latitude,
lng = position.coords.longitude;
this.push('thread.messages', {

    latlong: lat+","+lng,
}.bind(this));



}, function() {
console.warn('ERROR(' + err.code + '): ' + err.message);
});

-Karl Tiedt

Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/1a116241-5d89-4b09-a5da-d9c58442993d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Abhilash Sathe

unread,
Nov 22, 2016, 11:09:41 AM11/22/16
to Polymer
var self = this  and then in place of this I used self. Problem Solved ! Yes, scope was the problem.

Karl Tiedt

unread,
Nov 22, 2016, 12:33:50 PM11/22/16
to Abhilash Sathe, Polymer
I intentionally left that method out because it is the least correct way to fix the problem anymore.

-Karl Tiedt

Follow Polymer on Google+: plus.google.com/107187849809354688692

---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages