1

I have an application that uses Flow Router and its pub/sub mechanics. I also have a collection and template helpers. The code is, on client

Template.theCase.helpers({
    theCase: function () {
        var id = FlowRouter.getParam('id');
        var theCase = Cases.findOne({
            id: id
        });

        return theCase;
    }
});

and

{{#with theCase}}
  {{ id }}
{{/with}}

then, on server

Meteor.publish('theCase', function (id) {
    return Cases.findOne({
        id: id
    });
});

and finally, on both (lib)

FlowRouter.route('/case/:id', {
    subscriptions: function (params) {
        this.register('theCase', Meteor.subscribe('theCase', params.id));
    },
    action: function (params, queryParams) {
        return BlazeLayout.render('container');
    }
});

The problem, as I see it, is that helper returns undefined, since it's not allowed to find items in a collection by any other property than _id. How can I overcome it? I've read truckload of the official docs on pub/sub, helpers and routing, and I just can't find the solution. Any suggestions?

rishat
  • 8,206
  • 4
  • 44
  • 69

1 Answers1

1

You can query by any field. The helper returns undefined because it didn't find anything that matched.

This code is problematic:

Meteor.publish('theCase', function (id) {
    return Cases.findOne({
        id: id
    });
});

It should be: return Cases.find({id: id});

Publications must return a cursor or call this.ready()

Eliezer Steinbock
  • 4,728
  • 5
  • 31
  • 43
  • The problem I'm having is precisely because I want to use custom identity instead of mongo-generated `_id`. I have a property that is essentially a number and it increments. I know it may be the anti-pattern, but it's exactly the problem I'm attacking. – rishat Sep 07 '15 at 12:24
  • There could be reasons to use id. That's not a problem. I just edited my answer. I'd recommend printing logs to see that what you're actually expecting to find exists. – Eliezer Steinbock Sep 07 '15 at 12:25
  • I found a problem with your publication and edited my answer – Eliezer Steinbock Sep 07 '15 at 12:27
  • You were right, `Cases.find({...})` didn't return any value because `id` is String. Simply converting it to Number solved the issue. I'm dumb. – rishat Sep 07 '15 at 19:28