【问题标题】:How to filter a meteor cursor after the query and then publish the result如何在查询后过滤流星光标然后发布结果
【发布时间】:2016-01-15 18:02:29
【问题描述】:

我正在尝试流星仅发布用户有权访问的文档。 访问系统位于文档数据库之外,但如果用户具有访问权限,则类似 hasAccess(customuserID, _id) 的函数调用将返回 true。

问题是发布返回一个游标,所以我不能这样做:

Meteor.publish('theInvoices', function () {
  return Invoices.find().fetch().filter(function (doc) {
    return hasAccess(customerUserID, doc._id); // external access , filter
  });
});

或者当然这个函数返回一个过滤文档数组而不是一个游标所以问题是我如何在发布结果之前过滤查询结果(不使用数据库过滤器)或者我如何将一个文档数组转换为可发布游标?

谢谢

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    你可以试试这样的:

    Meteor.publish('theInvoices', function () {
      var invoices = Invoices.find().fetch().filter(function (doc) {
        return hasAccess(customerUserID, doc._id);
      });
      var invoiceIds = _.pluck(invoices, '_id');
      return Invoices.find({_id: {$in: invoiceIds}});
    });
    

    这只会找到所有允许的发票 ID,并根据该列表发布一个新游标。请记住这个isn't reactive

    还要注意customerUserID 应该在某个地方定义。

    【讨论】:

    • 谢谢。有没有办法让它反应?
    • 最简单的解决方案是添加一组可以访问每个发票文档的用户 ID。然后,您可以将该信息添加到选择器中。
    • 之前错过了您的链接,这有帮助。再次感谢。
    • 为了让它反应,你可以使用github.com/peerlibrary/meteor-reactive-publish 允许在服务器上自动运行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多