流星-铁路由器不等待onBeforeAction挂钩中的订阅数据 [英] Meteor - iron-router not waiting on subscription data in onBeforeAction hook

查看:47
本文介绍了流星-铁路由器不等待onBeforeAction挂钩中的订阅数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当请求的艺术家不可用时,我想设置一个Session变量(artistNotAvailable).不幸的是,该路由不起作用,因为它没有等待我的onBeforeAction挂钩中的订阅数据. artistAvailartist是未定义的,但不是因为数据库中没有艺术家,我认为这是订阅问题.我对其进行了调试,并在浏览器控制台中调用Artists.find().fetch();返回了undefined.

I want to set a Session variable (artistNotAvailable) when the requested artist is not available. Unfortunately, the route does not work, because it is not waiting on subscription data in my onBeforeAction hook. artistAvail and artist is undefined, but not because there is no artist in the DB, I think it is a subscription problem. I debugged it and calling Artists.find().fetch(); in the browsers console returned undefined.

这是我的路线:

this.route('eventPage', {
        path: '/event/:slug/:openDate?/:closeDate?',
        onBeforeAction: [teamFilter, function() {
            var event = this.data();
            if (event.outdoor) {
                this.subscribe('artists', this.params.slug);
                if (this.ready()) {
                    var artistAvail = Artists.findOne({eventId: event._id, openDate: moment(this.params.openDate).toDate(), closeDate: moment(this.params.closeDate).toDate()});
                    if ((!this.params.openDate && !this.params.closeDate) || typeof artistAvail === "undefined") {
                        var artist = Artists.findOne({
                            eventId: event._id,
                            openDate: {$lte: new Date()},
                            closeDate: {$gte: new Date()}
                        });
                        if (Artists.find().count() > 0 && artist) {
                            Session.set('artistNotAvailable', true);
                            this.redirect('eventPage', {
                                slug: this.params.slug,
                                openDate: moment(artist.openDate).format('YYYY-MM-DD'),
                                closeDate: moment(artist.closeDate).format('YYYY-MM-DD')
                            });
                        } else {
                            Session.set('noArtists', true);
                            this.redirect('overviewPage', {slug: this.params.slug});
                        }
                    } else {
                        this.subscribe('musicOutdoor', this.params.slug, moment(this.params.openDate).toDate(), moment(this.params.closeDate).toDate());
                        if (this.ready()) {
                            var guestsIds = new Array();
                            Guests.find({artistId: artistAvail._id}).forEach(function (guest) {
                                guestIds.push(guest._id);
                            });
                            this.subscribe('guestsOutdoor', guestIds);
                        } else {
                            this.render('loading');
                        }
                    }
                } else {
                    this.render('loading');
                }
            } else {
                this.subscribe('musicIndoor', this.params.slug);
                this.subscribe('guestsIndoor', this.params.slug);
                if (!this.ready()) {
                    this.render('loading');
                }
                this.next();
            }
            this.next();
        }],
        waitOn: function() { return [Meteor.subscribe('singleEvent', this.params.slug)]; },
        data: function() {
            return Events.findOne({slug: this.params.slug});
        }
    });

订阅:

Meteor.publish('artists', function(slug) {
    var event = Events.findOne({slug: slug});
    if (event) {
        return Artists.find({eventId: event._id});
    } else {
        return null;
    }
}); 

现在的问题是,当我用错误的openDatecloseDate访问eventPage时,即使数据库中有音乐人,我也将被重定向到overviewPage.如前所述,我调试了它,似乎是this.ready()无法正常工作. 我真的很想念.wait():-(

Now the problem is that when I access the eventPage with a wrong openDate or closeDate I will be redirected to the overviewPage, even though there are artists in the db. As already mentioned, I debugged it and it seems to be that this.ready() is not working. I really miss .wait() :-(

任何帮助将不胜感激.

推荐答案

铁路由器工作正常.

this.subscribe 类似于 Meteor.subscribe ,而Iron-Router无需等待.

this.subscribe is like Meteor.subscribe and Iron-Router doesn't wait for this.

waitOn 是一个函数,它将等待直到您返回的每个订阅都准备就绪,这就是为什么 this.ready() onBeforeAction 中为true的原因>

waitOn is the function that will wait until each of your returned subscriptions are ready that is why this.ready() is true in onBeforeAction

有几种方法可以做自己想做的事

There are several ways of doing what you want

  • 您可以像CodeChimp所说的那样通过在create/destroyed中使用模板来做到这一点
  • 您可以将路线分为不同的路线,以便在需要时进行重定向
  • 您可以使用铁路由器 查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆