redress
redress

Reputation: 1449

Meteor publish data of current user

I want to publish only the data created by the logged-in user. I have the following publish function:

    //server/collections/lists.js

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

but when I go to create a document as a logged-in user, the place on the DOM where the template should be rendered 'flashes' with the document Ive created, but then it disappears. What am I doing incorrectly?

Upvotes: 0

Views: 299

Answers (1)

David Weldon
David Weldon

Reputation: 64312

This is a symptom of attempting to insert on the client, not having an allow rule for the insert, and then having the insert rejected by the server a moment later. Try adding something like this under your server directory:

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);
  }
});

Note that you will need to customize the rule for your particular use case (e.g. you may not have an owner field). For example, you could just return userId to allow inserts from any logged in user.

Alternatively, you could not set up the allow and instead use a method to perform the insert. Generally speaking, that is my recommendation - see my answer to this question.

Upvotes: 2

Related Questions