7

I have the following server code:

Meteor.startup(function () {
  Meteor.publish("AllMessages", function() {
    lists._ensureIndex( { location : "2d" } );
    return lists.find();
  });
});

Meteor.methods({
  getListsWithinBounds: function(bounds) {
    lists._ensureIndex( { location : "2d" } );
    return lists.find( { "location": { "$within": { "$box": [ [bounds.bottomLeftLng, bounds.bottomLeftLat] , [bounds.topRightLng, bounds.topRightLat] ] } } } );
  }
});

and this client code:

Meteor.startup(function () {
  map = L.map('map_canvas').locate({setView: true, maxZoom: 21});
  L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
      attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  }).addTo(map);
    bounds = {};    
    map.on('locationfound', function(e){ 
      bounds.bottomLeftLat = map.getBounds()._southWest.lat;
      bounds.bottomLeftLng = map.getBounds()._southWest.lng;
      bounds.topRightLat = map.getBounds()._northEast.lat;
      bounds.topRightLng = map.getBounds()._northEast.lng;
      console.log(bounds);
      Meteor.call("getListsWithinBounds", bounds, function(err, result) {
        console.log('call'+result); // should log a LocalCursor pointing to the relevant lists
      });
    });
});

I get on my server logs:

Internal exception while processing message { msg: 'method',
  method: 'getListsWithinBounds',
  params: 
   [ { bottomLeftLat: 50.05008477838258,
       bottomLeftLng: 0.384521484375,
       topRightLat: 51.63847621195153,
       topRightLng: 8.3221435546875 } ],
  id: '2' } undefined

but I cant't figure out why...

George Katsanos
  • 13,524
  • 16
  • 62
  • 98
  • Can you check your meteor terminal at the same time this happens? – Tarang Mar 23 '13 at 05:53
  • The error msg on the bottom is the meteor terminal.. – George Katsanos Mar 23 '13 at 07:30
  • Have you indexed fields for a geospatial query? Meteor might hide mongodb errors – Tarang Mar 23 '13 at 07:32
  • This might help: http://stackoverflow.com/questions/11392566/mongo-geospatial-index-and-meteor – Tarang Mar 23 '13 at 07:52
  • Indeed I didn't ensureindex, I just added that, but I get a new kind of error:) Running on: http://localhost:3000/ Maximum call stack size exceeded Exited with code: 1 Maximum call stack size exceeded Exited with code: 1 Maximum call stack size exceeded Exited with code: 1 Your application is crashing. Waiting for file change. – George Katsanos Mar 23 '13 at 08:02
  • Try putting `lists._ensureIndex( { location : "2d" } );` in meteor startup instead. The call stack size exceeded means theres an infinite loop somewhere on server side code – Tarang Mar 23 '13 at 08:17
  • I have it in both startup and the method. I update my server side code in my question, have a look. – George Katsanos Mar 23 '13 at 08:28
  • Could you try moving it up so its not in the publish function but in the startup? It only needs to be run once, then not after as the index will be added to your collection. I think even if you've used it in your startup once it's not needed again unless you use a fresh db – Tarang Mar 23 '13 at 08:28
  • I had it there before and I had an error which I pasted originally in my question... – George Katsanos Mar 24 '13 at 13:39
  • Can you simulate a query manually in mongodb by running `meteor mongo` and performing it over on your lists collection and see if it gives a result if you used real values? – Tarang Mar 24 '13 at 13:48
  • Or could you paste what a document looks like in the lists collection? – Tarang Mar 24 '13 at 13:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26817/discussion-between-george-katsanos-and-akshat) – George Katsanos Mar 24 '13 at 16:41
  • Try a the call method after map.on with and array of bound instead of calling many times. – adrianj98 Mar 26 '13 at 03:41
  • You can also try youing the bound ={} inside the function. – adrianj98 Mar 26 '13 at 03:41
  • Αkshat helped me find a solution so he can post it as an answer so that I'll accept it.. – George Katsanos Mar 26 '13 at 19:10

1 Answers1

16

You cannot return a Collection cursor - it's unable to be converted into an EJSON object. Return the results of your query as an array.

e.g.

return Lists.find(...).fetch();
cazgp
  • 1,538
  • 1
  • 12
  • 26