Vadim Novozhilov
Vadim Novozhilov

Reputation: 855

How to push specific users in Mongo Collection?

I'm working on an app in Meteor, let's call it meetups. I've already done basic crud and now I'm stucked with request action when a user sends a request for invite for a meetup (I use accounts-password package to handle auth, if it means anything). I want to show meetup's owner which users would like to visit this meetup, so it's how I designed it: I created a 'Join' button in my template and it works perfectly when I make an event in my controller like this:

'click .join': function(event) {
    var params = {
      user: Meteor.userId(),
      username: Meteor.user().username
    }
    toastr.success('You've send a request');
    Router.go('/meetups');
}

Since I handle this events fetching user's params, it's very easy to show his username, I do it like this:

<p>{{username}} sent a request to join your meetup</p> <button class="btn-primary btn-xs yes">Ok</button> <button class="btn-danger btn-xs no">No</button>

I don't want to just show this user, I want to let meetup's owner also manage requests (that's why I created two buttons). Basically, it's just a simple crud, isn't it? That's why I created a new Mongo collection called 'Guests' and my 'invite event now looks like this:

'click .join': function(event) {
        var params = {
          user: Meteor.userId(),
          username: Meteor.user().username
        }
    Meteor.call('join', params);
    toastr.success('Ok');
    Router.go('/meetups');
}

In case I use call function, I have a method to handle it:

Meteor.methods({
    'join': function (params) {
        Guests.insert(params);
    }
});

I also created anothe template called guests:

<template name="guests">
  {{#each guests}}
    <p>{{username}} sent a request to join your meetup</p> <button class="btn-primary btn-xs yes">Ok</button> <button class="btn-danger btn-xs no">No</button>
  {{/each}}
</template>

and included it in my meetups template like this: {{> guests}}. As you can understand since that I see no usernames. I used an iteration through guests collection and try to fetch username because it worked just fine withound defining a separate collection. So my question is how the heck can I push specific users (in my case those who clicked join button) and show their usernames? Any help would be appreciate.

Upvotes: 1

Views: 54

Answers (1)

0x860111
0x860111

Reputation: 386

<template name="guests">
  {{#each guests}}
    <p>{{username}} sent a request to join your meetup</p> <button class="btn-primary btn-xs yes">Ok</button> <button class="btn-danger btn-xs no">No</button>
  {{/each}}
</template>

How about add guests helper in guests template? You didn't define it.

Template.guests.helpers({
    guests: function () {
        return Guests.find();
    }
});

Upvotes: 1

Related Questions