【问题标题】:Meteor Publish & Subscribe Not returning results using selector流星发布和订阅不使用选择器返回结果
【发布时间】:2016-10-29 19:13:39
【问题描述】:

我有以下代码:

import { Meteor } from 'meteor/meteor';
import { Items } from './collection';

    if (Meteor.isServer) {
      Meteor.publish('items', function(options, owner) {

        let selector = {
          $and: [{ ownerId: owner}]
        }

        return Items.find(selector, options);

      });
    }

在客户端我有:

this.subscribe('items', () => [{
      limit: this.getReactively('querylimit'),
      sort: {dateTime: -1}
    },
    this.getReactively('ownerId')
    ]);

以上不返回任何结果。但是,当我将 return 语句更改为以下内容时,它可以工作!

return Items.find({ ownerId: '7QcWm55wGw69hpuy2' }, options); //works !!!

我对 Mongo/Meteor 查询选择器不是很熟悉。将查询作为变量传递给 Items.find() 似乎搞砸了。谁能帮我解决这个问题!

谢谢

【问题讨论】:

    标签: angularjs mongodb meteor angular-meteor


    【解决方案1】:

    您正在尝试将函数作为选择器传递,但这是行不通的。函数不能被序列化并从客户端发送到服务器。相反,您需要分别评估 optionsowner。这是一个例子:

    var owner = this.getReactively('ownerId');
    var options = {
      limit: this.getReactively('querylimit'),
      sort: {dateTime: -1}
    };
    
    this.subscribe('items', options, owner);
    

    请注意,发布的文档不会arrive in sorted order,因此除非您使用limit,否则sort 在这里没有帮助。

    另请注意,如果您需要在所有者或查询限制更改后重新运行订阅,则需要在 autorun 内进行订阅。

    这是改进实施的开始:

    Meteor.publish('items', function(options, owner) {
      // DANGER! Actually check this against something safe!
      check(options, Object);
    
      // DANGER! Should any user subscribe for any owner's items?
      check(owner, Match.Maybe(String));
    
      // Publish the current user's items by default.
      if (!owner) {
        owner = this.userId;
      }
    
      return Items.find({ ownerId: owner }, options);
    });
    

    【讨论】:

    • 谢谢。我现在在手机上,所以我现在无法检查。但是,我想知道为什么我能够在服务器端使用 console.log 来打印参数。 console.log(selector) 打印我在第二个 return 语句中粘贴的 ownerId 字符串。
    • 再次感谢,但这似乎并不能解决问题
    • 当您将console.log 参数传递给publish 函数时,您会得到什么?您是否将subscribe 放入autorun 中?
    • 糟糕!原谅我,我的坏。在更改所有内容时,我将代码从订阅的回调函数中移出。它现在按预期工作!非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 2014-05-04
    • 2018-12-18
    • 2018-12-26
    • 2014-06-16
    相关资源
    最近更新 更多