【问题标题】:Meteor publish data of current user流星发布当前用户数据
【发布时间】:2015-02-15 23:58:20
【问题描述】:

我只想发布登录用户创建的数据。我有以下publish 函数:

    //server/collections/lists.js

    Meteor.publish('lists', function(){
        if(this.userId){
            return listCollection.find({createdBy: this.userId});
        } else {
            this.ready();
        }
    });

但是当我以登录用户的身份创建文档时,DOM 上应该呈现模板的位置与我创建的文档一起“闪烁”,但随后消失了。我做错了什么?

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    这是尝试在客户端上插入的症状,没有allow rule 用于插入,然后稍后服务器拒绝了插入。尝试在 server 目录下添加类似的内容:

    listCollection.allow({
      insert: function (userId, doc) {
        // the user must be logged in, and the document must be owned by the user
        return (userId && doc.owner === userId);
      }
    });
    

    请注意,您需要为您的特定用例自定义规则(例如,您可能没有 owner 字段)。例如,您可以只使用return userId 来允许任何登录用户的插入。

    或者,您不能设置allow,而是使用method 来执行插入。一般来说,这是我的建议 - 请参阅我对 this question 的回答。

    【讨论】:

    • createdBy Meteor 约定,还是必须在集合上定义该字段?我从一个教程的 sn-p 得到它
    • 您可以随意称呼它。我相信owner 更常见,但这只是个人喜好问题。
    • 但要清楚,我在我的 listCollection = new Meteor.Collection('listCollection') 定义中添加字段 owner 并拥有它 return Meteor.userId()
    • 不,除非您使用包来管理您的架构。您的集合定义应该只是listCollection = new Meteor.Collection('listCollection')。集合不知道它自己的模式 - 这取决于允许/拒绝或强制执行的方法。我建议您通过tutorial 了解这些操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多