How should judge whether user have role?


#1

Now,i am making an sample forum. So, i want’t user can edit their own post. so, i write this code:

{{#if ownId}}to this point {{/if}}

but , this can’t work .beacuse, i don’t konw how to use this
my wrong code:
Template.user.helpers( { ownId: function () { return this.userId === Meteor.userId() }})
So, what should i do to judge user?


#2

Assuming you have a Mongo.Collection named Posts, and Posts has some kind of authorId, you can do:

Posts.allow({
  update: function(userId, doc) {
    return userId === doc.authorId;
  }
})

#4

Thank you~ I will try it :blush:


#5

Typical use case (given that your Posts collection has title and userId fields):

{{#each posts}}
  <!-- In here, "this" points to the current post in the loop -->
  <div>{{title}} {{#if ownId}}(this is my own post){{/if}}</div>
{{/each}}
Template.postList.helpers({
  posts: function () {
    return Posts.find({});
  }
  ownId: function () {
    return this.userId === Meteor.userId()
  }
});

#7

Thank for your advice.
There is two collection in this app.One is user, the other is post.
each block have been used in another template to show posts,and for route.
if i dont’t use each , could if block work?:flushed:


#8

A typical use case without #each:

{{#with onePost}}
  <!-- In here, "this" points to the post returned by onePost -->
  <div>{{title}} {{#if ownId}}(this is my own post){{/if}}</div>
{{/each}}
Template.postList.helpers({
  onePost: function () {
    return Posts.findOne(...);
  }
  ownId: function () {
    return this.userId === Meteor.userId()
  }
});

#9

Ok , that works . Thank you for your idea :grinning: