user5030225
user5030225

Reputation:

Meteor, how do I only return items from a collection that the user owns

I need my app to only return the "ideas" that the user owns (each idea has a property called "owner" with the usedID in it). This seems to be returning nothing, what am I doing wrong?

if (Meteor.isServer) {
  // Only publish tasks that are public or belong to the current user
  Meteor.publish("ideas", function () {
    return Ideas.find({owner: this.userId });
  });
}

Upvotes: 0

Views: 339

Answers (2)

Total Shaw
Total Shaw

Reputation: 503

I think you may have something wrong in your understanding of Meteor.publish and Meteor.subscribe. In your server, what publish return is only a subset of your collection, which means you could get these data in your client if you subscribe it. here has more details.

// Here is publish in server
if (Meteor.isServer) {
  // Only publish tasks that are public or belong to the current user
  Meteor.publish("ideas", function () {
    return Ideas.find({owner: this.userId });
  });
}

// Here is subscribe in client, to get your data set
// You could write these code in Meteor.startup or other place before you using "ideas" data set
if (Meteor.isClient) {
  Meteor.subscribe("ideas");
}

// In your template.helpers
// You only publish and subscribe, which means ideas of current user
// are mirrored in mini mongo in client
// add a helper to your template could use these data in your template

Template["yourIdeasTemplate"].helpers({
  ideas: function() {
    // Because you only subscribe "ideas" whose owner is current user
    // So selector in find is not necessary
    // All the record in your ideas collection is current user's ideas
    return Ideas.find();
  }
});
<template name="yourIdeasTemplate">
  <ul>
   {{#each ideas}}
    <li>{{ideas.owner}}</li>
    <!--Your code-->
   {{/each}}
  </ul>
</template>

That's all, I think that would help you :-)

Upvotes: 1

Luna
Luna

Reputation: 1178

Second EDIT with details:

You need to return in your template helper.

This is where you define what you want to show in your template:

Template.yourTemplateName.helpers({
    Ideas: function () { 
            selector = {owner: Meteor.userId()};
            options = {sort: {createdAt: -1}};
            return Ideas.find(selector, options);
        }
    }
});

Your template would look like below (not the "idea" template) Even though it looks like we fetch all the "ideas" here, we don't because we defined which ideas to fetch above, in our template helper

<template name="yourTemplateName">
    <ul class="all-ideas">
        {{#each ideas}}
            {{> idea}}
        {{/each}}
    </ul>
</template>

I assume you already have your idea template.

and your publish is okay like this in your server: The code below only says "I allow this collection to be available on the client side

  Meteor.publish("Ideas", function () {
    return Ideas.find();
  });

Hope this is more clear. Let me know if you have any other questions. If this doesn't work, please update your questions with related codes.

Upvotes: 1

Related Questions