【问题标题】:Filter data for user access with Meteor.publish使用 Meteor.publish 过滤用户访问的数据
【发布时间】:2017-11-14 23:52:15
【问题描述】:

我需要帮助:我无法让 (Meteor.publish) 过滤页面数据并应用用户(所有者)添加的许可。

现在我向您展示我在 Coffescript 中编写的代码(publish.coffee 和使用包的 DataTable 的 JS (aldeed:tabular - https://atmospherejs.com/aldeed/tabular)

  1. publish.coffee
Meteor.publish 'reportPage', ->
     userId = @userId
       reportpage = Dealers.find('owner': userId)
     if Dealers
       return reportPage
  @ready()
  1. reportpage.js
TabularTables ={};

Meteor.isClient && Template.registerHelper('TabularTables', TabularTables);

TabularTables.Dealers = new Tabular.Table({
  name: "Dealers",
  collection: Dealers,
  columns:[
     {data: "name", title: "Name"},
     {data: "p_iva", title: "Partita IVA"},
  ]
}, { 
    waitOn: function() {
        return [
            Meteor.subscribe("reportPage"),
        ]
    },
    path: "/reportPage"
});
  1. reportpage.html
<template name="reportpage">
    {{> tabular table=TabularTables.Dealers class="table table-striped table-bordered table-condensed"}}
</template>

我该如何解决? 请帮我! :(

【问题讨论】:

  • 我不太了解 CoffeeScript,但 .find 接受 JSON 对象。因此,在发布方法中,Dealers.find({'owner': userId})。否则,看起来您的 HTML 在订阅数据之前就已渲染,请尝试在路由上订阅数据或在渲染表之前检查是否订阅了数据。
  • 我试图应用这个 Meteor.publish "reportpage", -> Dealers.find { owner: this.userId }, 但是所有用户的数据总是出现:( 在我的情况下它只是必要的显示登录用户(所有者)的输入数据
  • 从您的应用程序中删除自动发布包。
  • 已删除,但无法正常工作:(
  • 我发现这是我的情况,但我不知道该放在哪里:{userId: Meteor.userId ()} 看这里:github.com/aldeed/meteor-tabular/issues/13“你会通过 { userId: Meteor.userId()} 作为表上的选择器属性。”

标签: javascript meteor coffeescript


【解决方案1】:

尝试使用selector 过滤表格使用的结果。

TabularTables.Dealers = new Tabular.Table({
  name: "Dealers",
  collection: Dealers,
  columns:[
    {data: "name", title: "Name"},
    {data: "p_iva", title: "Partita IVA"},
  ], 
  selector: function (userId) {
    if (!!userId) {
      return {owner: userId}
    }
  },
}

更多信息请参考Displaying Only Part of a Collection's Data Set

【讨论】:

  • 我试过了,但它不能正常工作 我在浏览器日志中看到“警告”,并且表中出现了其他值,仅包括所有者 userId。我使用文档:selector(userId) { return {owner: userId} } }); 然后我阅读了文档,我已经应用了它,但是它不起作用,它返回表中所有用户的所有数据,@aldeed 在问题 #13 中提出的解决方案将解决使用selector 的问题,但我不知道如何应用它
  • 对不起,我试过你的解决方案,我错过了`},'怎么在你的解决方案中遗漏了,因为我猜代码没有正确执行,但是,所有用户数据都显示出来了:(跨度>
【解决方案2】:

我是这样解决的:

Template.reportpage.helpers({
  selector () {
    if (!!Meteor.userId()) {
      return { owner: Meteor.userId() };
    }
  },
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多