【问题标题】:MeteorJS publish query not workingMeteorJS 发布查询不起作用
【发布时间】:2015-09-25 00:58:55
【问题描述】:

我尝试在此查询中使用 Meteor.publish(服务器端):

return Meteor.users.find({_id:{$ne:this.userId}});

当我在客户端使用 Meteor.subscribe 进行查询时,它可以工作:

return Meteor.users.find({_id:{$ne:Meteor.userId()}});

那么为什么它不能在服务器端工作...看来我只能在客户端查询它一次...问题是,我不想下载整个集合,因为我会超过20,000 名用户。发布方法是否不允许“$”查询?

另外,我怎样才能将它附加到我的以下查询语句:

return Meteor.users.find({"profile.loc":{ $near: [ to[0].profile.loc.lat, to[0].profile.loc.lon ], $maxDistance: (1/111.2)*250}});

【问题讨论】:

  • 一切都按预期工作。如果您不希望在客户端上发布所有 20000 个用户,则不要发布它。阅读有关发布/订阅的更多信息。

标签: javascript meteor meteor-blaze meteor-accounts


【解决方案1】:

正如上面的回答所说,如果你按照你写的去做,你无论如何都会发布 19,999 个用户。

不过,您的问题分为两部分,您应该通过一个查询真正解决这两个问题:要做到这一点,您应该有一个带有参数的出版物:

    Meteor.publish('users', function(location) { 
         return Meteor.users.find(
                        {_id: {$ne: this.userId}, 
                          "profile.loc":{ 
                           $near: [ location.lat, location.lon ], 
                           $maxDistance: (1/111.2)*250}
                           }
                         });

这将筛选服务器上符合位置条件的用户。

在客户端你订阅它:

location = {lon: 12.123, lat: 110.2};
Meteor.subscribe('users', location);

或您选择的对象。

【讨论】:

    【解决方案2】:

    发布方法确实允许 $ 查询。您的出版物所做的是发布所有 id 不等于 this.userId 的用户。如果您有 20,000 个用户,此方法将发布 19,999 个用户的数据。

    如果您只想发布当前用户的数据,请尝试:

    Meteor.publish('userData', function() { return Meteor.users.find(this.userId) };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      相关资源
      最近更新 更多