Search code examples
javascriptmeteorgeolocation

Exception from sub geo id XXX TypeError: Cannot read property 'lng' of null


Im using Mdg:geolocation and have the below application.

To find user's current location and retrieves posts that are within a distance from user (Got error, page stuck at loading screen). lng of null implies that loc is undefined.

Pub

  Meteor.publish('geo', function(loc) {     
    return Posts.find({
      "loc": {            // error line for server.js 13:30
        $near: {
          $geometry: { type: "Point", coordinates: [loc.lng, loc.lat] }, 
           .....


Router

Router.route('/near/:postsLimit?', { 
  name: 'nearPosts',
  waitOn: function(){ 
    return Meteor.subscribe('geo');
  }
});


Error message


Solution

  • In your route (reproduced below), there is only a this.params.postsLimit, no this.params._id

    Router.route('/near/:postsLimit?', { 
      name: 'nearPosts',
      waitOn: function(){ 
        return Meteor.subscribe('geo', this.params._id); //to change reference id
      }
    });
    

    Therefore the publish function is throwing an error because there is no loc to get loc.lng

    Your route contains /near/:postsLimit?. For this.params._id, you need something like /near/:_id or /near/:_id/:postsLimit?, which would give you both. See docs.

    EDIT: It looks like you need to have some location data object containing latitude and longitude ready to pass into your subscription like so:

    Router.route('/near/:postsLimit?', { 
      name: 'nearPosts',
      waitOn: function(){ 
        return Meteor.subscribe('geo', locationObject);
      }
    });
    

    It is up to you to figure out what locationObject to use.