【问题标题】:Using Meteor.methods and Meteor.call使用 Meteor.methods 和 Meteor.call
【发布时间】:2013-03-12 20:57:32
【问题描述】:

我有以下服务器代码:

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] ] } } } );
  }
});

还有这个客户端代码:

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
      });
    });
});

我得到了我的服务器日志:

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

但我不知道为什么......

【问题讨论】:

  • 发生这种情况的同时你能检查你的流星终端吗?
  • 底部的错误信息是流星终端..
  • 您是否为地理空间查询编制了索引字段? Meteor 可能会隐藏 mongodb 错误
  • 这可能会有所帮助:stackoverflow.com/questions/11392566/…
  • 确实我没有确保索引,我只是添加了那个,但我得到了一种新的错误:) 运行:localhost:3000 超出最大调用堆栈大小退出代码:1 最大调用堆栈大小超出 退出代码:1 超出最大调用堆栈大小 退出代码:1 您的应用程序正在崩溃。等待文件更改。

标签: javascript meteor


【解决方案1】:

您无法返回 Collection 游标 - 它无法转换为 EJSON 对象。将查询结果作为数组返回。

例如

return Lists.find(...).fetch();

【讨论】:

  • 对,但这不是反应式数据源。在这种情况下,也许你最好使用Meteor.publish 而不是Meteor.methods?...
猜你喜欢
  • 2013-01-23
  • 2012-08-27
  • 2014-02-09
  • 1970-01-01
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
相关资源
最近更新 更多