0
votes

In my meteor app I'm implementing functionality to send messages between users. In my app I have a route and template that displays all messages sent to the user.

The controller code:

this.UserMessagesController = RouteController.extend({
    template: "UserMessages",
    waitOn: function() {
        return Meteor.subscribe("userMessages");
    },
    data: function() {
        return {
            "messages": UserMessages.find({to: Meteor.userId()}, {sort: {time: -1}} )
        }
    }
});
}
});

On the server side I have a publish functions

Meteor.publish("userMessages", function() {
    var userId = this.userId;
    return UserMessages.find({to: userId}, {});
});

Also I have a global subscription to unread messages:

Deps.autorun(function() {
    if (Meteor.user()) {
        Meteor.subscribe("unreadMessages");
    }
});

Meteor.publish("unreadMessages", function() {
    return UserMessages.find({to: this.userId, read: undefined}, {});
});

The problem is: I can see only unread messages rendered on template (and unread message only in client minimongo collection as well)

But if I remove filter {to: userId} from publish function or remove subscription to unread messages I can perfectly see each message!

PS: I tried to debug publish function setting breakpoint and evaluating UserMessages.find({to: userId}, {}).fetch() and I got absolutely correct array consisting of 15 messages.

PPS: I got no error messages on the server side as well as on client side

What might be a problem?

2
Can you post your unread messages subscription? You said "...or remove subscription to unread messages ..." I suspect that subscription is somehow overwriting the data you want here. - Shaded
Yeah, Deps.autorun(function() { if (Meteor.user()) { Meteor.subscribe("current_user_data"); Meteor.subscribe("unreadMessages"); } }); - Rem

2 Answers

0
votes

I think you need:

Meteor.publish("userMessages", function(id) {
    return UserMessages.find({to: id}, {});
});

and then pass the id parameter in the subscribe request

waitOn: function() {
        return Meteor.subscribe("userMessages", Meteor.userId());
    },

but for troublshooting these stuff, your best friend is MeteorToys to debug stuff: they have a free version, get it with

meteor add meteortoys:allthings

and hit CRTL + M (on Mac OS)

it will show you everything that is sent to the browser so you can troubleshoot much more easily.

0
votes
  1. Subscribe

    this.UserMessagesController = RouteController.extend({
    template: "UserMessages",
    
    subscriptions: function(){
    
    return [
        Meteor.subscribe("user.message")
      ]
    },
    
    data: function() {
    
     return UserMessages.find();
    
    })
    
  2. Publish

    Meteor.publish("user.message", function(){
    
      var criteria = {};
      var projection = {};
    
      var userId = this.userId;
      _.extend(criteria, {to: userId});
      _.extend(projection, { 
        sort: {
          time: -1
          } 
       });
    
      return UserMessages.find(criteria, projection);
    
    });