Meteor发布当前用户的数据

Meteor publish data of current user

本文关键字:数据 用户 布当前 Meteor      更新时间:2023-11-25

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

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

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

这是一种症状,即试图在客户端上插入,但没有插入的允许规则,然后插入被服务器拒绝。尝试在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,而是使用一个方法来执行插入。一般来说,这是我的建议——请看我对这个问题的回答。