Search code examples
javascriptmeteoriron-router

Display 404 not found template for 'slugs' when publication returns empty cursor - Meteor - iron-router


My question is related to this problem, because I have the same issues. If I have a route like this:

this.route('userPage', {
    path: '/user/:username',
    waitOn: function() { return [Meteor.subscribe('userPage', this.params.username)];
    },
    data: function() {
        return Users.findOne({username: this.params.username});
    }
});

and my subscription looks like this:

Meteor.publish('userPage', function(username) {
    return Users.find({username: username}, {fields: {'profile': 1, 'username': 1}});
});

The 404 page will be rendered (this is fine), but the progress-bar hangs at 98% or so (why?). If I have a route like this:

this.route('chat', {
        path: '/chat/:slug',
        waitOn: function() { return [ Meteor.subscribe('chat', this.params.slug), Meteor.subscribe('chatAvatars', this.params.slug) ]; },
        data: function() { return Chat.findOne({slug: this.params.slug}); }
    });

and my subscriptions like this:

Meteor.publish('chat', function(slug) {
    return Chat.find({slug: slug});
});

Meteor.publish('chatAvatars', function(slug) {
    var chat = Chat.findOne({slug: slug});
    if (chat) {
        return Users.find({_id: {$in: chat.participants}}, {fields: {'profile.image': 1, 'username': 1}});
    } else {
        return null;
    }
});

No 404 not found template will be rendered. Why? Do I have to check in my onBeforeAction if the chat exists?


Solution

  • You have forgotten to set the publish as ready

    Meteor.publish('chatAvatars', function(slug) {
    var chat = Chat.findOne({slug: slug});
    if (chat) {
        return Users.find({_id: {$in: chat.participants}}, {fields: {'profile.image': 1, 'username': 1}});
    } else {
         this.ready();//<============here
    }
    });