0

I'm trying to show posts by the current logged in user. I tried the same method which I used for showing comments under related post's page but doesn't work for logged in user's posts.

Template.myPosts.helpers({
    posts: function () {
        selector = {userId: this.userId};
        options = {sort: {createdAt: -1}};
        return Posts.find(selector, options);
    }
});

I also tried the code below in router.js (iron router package installed):

this.route('myPosts', {
        path:'/my-posts',
            data:function(){
                return Posts.find({userId: this.userId})
        }             
                            });
})

If the code above is not close to how it should be, any hints on how to proceed will be well appreciated. This is a project for only learning purposes.

Thanks!

Luna
  • 1,168
  • 8
  • 17
  • possible duplicate of [In Meteor, how can I query only the records of a given subscription?](http://stackoverflow.com/questions/27748100/in-meteor-how-can-i-query-only-the-records-of-a-given-subscription) – Sergio Tapia Jul 30 '15 at 23:47
  • Do you have the subscribe and publish code too? – k.chao.0424 Jul 31 '15 at 00:56
  • @k.chao.0424 I tried that as well. I've been trying different methods with different (famous) packages for the last 4-5 hours and still failing. – Luna Jul 31 '15 at 02:49

2 Answers2

1

Are you sure the context of your helper has a userId property? Maybe you want this:

Template.myPosts.helpers({
  posts: function () {
    selector = {userId: Meteor.userId()};
    options = {sort: {createdAt: -1}};
    return Posts.find(selector, options);
  }
});

It's also worth trying: Posts.find({userId: Meteor.userId()}).fetch() in the web console to see if you have any such posts actually published to the client.

David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • Thanks for the comment. I have this for userId property: Template.secret.helpers({ isOwner: function () { return this.owner === Meteor.userId(); } – Luna Jul 31 '15 at 03:26
  • And this inside Posts.insert " owner: Meteor.userId(), ". Should I use "owner" instead of userId? – Luna Jul 31 '15 at 03:27
  • HOLY SNAP! It worked! What a stupid mistake... It took my 4-5 hours... Thank you very much again for your help David! You answered all my questions until now. I really appreciate it. – Luna Jul 31 '15 at 03:30
0

It looks like a problem with your publications and subscriptions. Make sure you have publish and subscribe properly.

On /server/publications.js:

Meteor.publish("postsPub", function (selector, options) {
  return Posts.find(selector, options);
});

On your router:

this.route('myPosts', {
  path:'/my-posts',
    waitOn : function () {
      selector = {userId: Meteor.userId()};
      options = {sort: {createdAt: -1}};
      return Meteor.subscribe('postsPub', selector, options);
    },
    data:function(){
      return Posts.find({userId: Meteor.userId()});
    }
});
FullStack
  • 5,902
  • 4
  • 43
  • 77
  • Autopublish and insecure are both removed. Is it possible to do this without installing them back? I've been trying for the last 4-5 hours... Yes. 4-5 hours... HALP! – Luna Jul 31 '15 at 02:47
  • Thanks for the answer. Unfortunately returns an empty page. Tried changing "Meteor.userId()" with this.userId (which is used in different places). Trying a few other things now. – Luna Jul 31 '15 at 03:20