【问题标题】:How do I make sorting work?如何使排序工作?
【发布时间】:2015-08-16 06:24:58
【问题描述】:

将所有帖子(地理标记)拉到一定距离内的发布/订阅不起作用。目的是获取用户当前位置,然后使用pub(服务器计算)拉取相关数据+在router上使用sort进行排序。

js 中的助手/事件(这部分,我还在调整它,看看有什么用)

Template.postsList.onCreated(function() {
  this.interval = Meteor.setInterval(function (){
    var currentLocation = Geolocation.latLng();
      if(currentLocation) {
        Session.set('currentLocation', currentLocation); 
      }
    }, 2000
  );   
  Session.set('postSubmitErrors', {});
});

Template.postsList.helpers({
  loc : function () { 
    return Session.get('currentLocation');... // may not be needed if get in router

Template.postsList.events({
  postsByDistance : function() {  // may not be needed if get in router
    var loc = Session.get('currentLocation');...


终端给出的错误是

来自子帖子的异常ByDistance id bsWXKgw5QboNzCRXw 错误: 轮询查询时出现异常 {"collectionName":"posts","selector":{"loc":{"$near":{"$geometry":{"type":"Point","coordinates":{"currentLocation":{" lat":xxx,"lng":xxx}}},"$maxDistance":500}}},"options":{"transform":null}}: $near 需要一个点,给定 { 类型:“点”,坐标:{ 当前位置:{ lat: xxx, lng: xxx } } }

如果我将 pub 行更改为 [anything.lng,anything.lat],它会显示 lat 或 lng of undefined 当我更改 function() 中的参数时,有时会收到 Exception while polling query meteor 错误。



15 年 8 月 16 日 - 建议后更新: 新酒吧

  Posts._ensureIndex({'loc' : '2dsphere'});  

  Meteor.publish('postsByDistance', function(options) {     
    check(options, Object);                          
    return Posts.find({
      loc: { 
        $near: {
          $geometry: {
            type:        "Point", 
            coordinates: [currentLocation.lng, currentLocation.lat]  

错误现在表明 currentLocation 未定义。但是我确实在助手中定义了? 我还认为在错误中它说我有"options":{"transform":null}},如果正确不应该出现?

【问题讨论】:

  • 从您发布的错误看来,您正在传递对象作为coordinates 的值。你不应该使用一个包含两个数值的数组吗?
  • @apendua 我试图用 currentLocation 传递它,它定义为 = geolocation.latlng() 应该返回一个数组。您能否建议我如何排除故障或测试错误?任何示例代码也会很棒

标签: javascript meteor geolocation iron-router


【解决方案1】:

在您的 mongo 查询中,coordinates 字段绝对应该是一个包含两个数字的数组。但据我了解,这是您已经尝试过的。由于其他原因,它不起作用。

看起来您的问题是由简单的“竞争条件”引起的。即,您第一次订阅postsByDistance 时,会话变量currentLocation 尚未设置,这就是您可能获得undefined 纬度和经度值的原因。

另外 - 这可能是一个拼写错误 - 查看您的代码,您将一个带有 currentLocation 的对象提交给订阅。另一方面,在您相应的发布功能中,您将整个内容称为currentLocation,但它应该是

 currentLocation.currentLocation.lng
 currentLocation.currentLocation.lat

使用您当前的变量设置。我建议您更改这些名称,因为这可能会导致很多不必要的错误。

【讨论】:

  • 所以我想应该是[cl.cl.lng, cl.cl.lat] 格式吧?我需要更改帮助文件中的任何内容吗?我应该如何更改命名格式?
  • 命名格式很好,我只是指您发布函数的第一个参数。将其命名为 currentLocation 会产生误导,因为您真正拥有的是带有 currentLocation 字段的 options 对象。
  • 我已经更新并且出现了新的错误,尽管它是 cl.cl.lat/lng 或 cl.lat/lng。知道我应该如何测试它吗?浏览器上没有显示错误。还说 currentLocation 没有定义。 (我标记了上面的错误行)
  • 正如我之前所说,发生这种情况是因为Session.get('currentLocation') 将在您第一次调用它时返回undefined。您需要在客户端(Meteor.subscribe 之前)或服务器(在发布函数内部)上使用条件语句,以确保您的应用程序可以处理该边缘情况。我建议实际上在客户端和服务器上都这样做。
猜你喜欢
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多