【问题标题】:iron-router not waiting on meteor.user() to return铁路由器不等待meteor.user()返回
【发布时间】:2016-07-29 02:14:49
【问题描述】:

已解决但正在寻找改进,请查看答案

    function completedProfile() {
        if (Meteor.user() && Meteor.user().profile.primary_email && Meteor.user().profile.display_name && Meteor.user().university_id) {
            return true;
        } else {
            return false;
        }
    }

    Router.map(function() {
        this.route('landing', {
            path: '/', // match the root path
            onBeforeAction: function() {
                this.subscribe('users').wait();
                if (this.ready() && completedProfile()) {
                    this.render('bulletin')
                } else {
                    this.redirect('splash');
                }
            }
        });
    });

页面将重定向到我的初始模板,因为 completeProfile 函数为 false(if 语句中的所有字段都为空)。

尝试 2(在填充字段后挂起):

    Router.map(function() {
        this.route('landing', {
            path: '/', // match the root path
            action: function() {
                if (this.ready() && completedProfile()) {
                    this.redirect('bulletin')
                } else {
                    this.render('splash');
                }
            },
        });

尝试 3:

    function completedProfile() {
        if (Meteor.user() && Meteor.user().profile.primary_email && Meteor.user().profile.display_name && Meteor.user().profile.university_id) {
            return true;
        } else {
            return false;
        }
    }

    Router.map(function() {
                this.route('landing', {
                    path: '/', // match the root path
                    action: function() {
                        console.log(Meteor.user().profile);  //this fails
                        console.log(this.ready(), completedProfile()); //this then returns as expected
                        if (this.ready() && !completedProfile()) {
                            this.redirect('splash');
                        } else {
                            this.render('bulletin')
                        }
                    },
                });

                this.route('splash', {
                    path: '/splash', // match the root path
                });

                this.route('bulletin', {
                    path: '/bulletin', // match the root path
                });

这真的很奇怪。 Meteor.user 的日志导致错误,然后 Meteor.user() 加载给我一个适当的 completedProfile() 返回。错误是Exception in callback of async function: TypeError: Cannot read property 'profile' of undefined

【问题讨论】:

  • Meteor.user() 返回undefined 时,console.log(Meteor.user().profile); 会引发错误(您正在尝试读取undefined 的属性'profile')。试试if (Meteor.user()) { console.log(Meteor.user().profile) }

标签: javascript meteor iron-router


【解决方案1】:

看起来您正在重定向到名为 splash 的路由。你没有包括它,但我会假设它存在。重定向后,您就完成了 landing 路由的所有逻辑,因此一旦用户登录,completedProfile() 就不会重新运行。

试试这个:留在landing 路线,但render() 是一个启动页面,直到用户登录。

if (this.ready() && completedProfile()) {
    this.render('bulletin')
} else {
    this.render('splash');
}

我不认为this.subscribe('users').wait(); 有帮助。我会删除它。我想你也想使用action 而不是onBeforeAction,因为你自己打电话给render()

另请参阅:https://github.com/EventedMind/iron-router/issues/91。听起来instant login 很适合您的用例。

【讨论】:

  • 如果我这样做 if(ready && complete) redirect(bulletin) else render(splash) 它仍然挂在启动页面上(这一切都在行动中)。我现在来看看即时登录。
  • 您确定stop() 是什么使它起作用吗?我认为将redirect() 更改为render() 是使它起作用的原因,因为您停留在landing 路线上并且它会做出反应(重新渲染)。
  • 如果我在用户完成个人资料字段后立即删除停止,它将重定向到公告,是被动的,什么不是。我希望他们点击启动页面上的完成/继续按钮,以便他们查看信息。
【解决方案2】:

目前的工作解决方案:

    function completedProfile() {
        if (Meteor.user() && Meteor.user().profile.primary_email && Meteor.user().profile.display_name  && Meteor.user().profile.university_id) {
          return true;
        } else {
          return false;
        }
    }

    Router.map(function() {
        this.route('landing', {
            path: '/', // match the root path
            action: function() {
                if (this.ready()) {
                    if (!completedProfile()) {
                        this.render('splash');
                        this.stop();
                    } else {
                        this.render('bulletin')
                    }
                }    
            }
        }
    });

【讨论】:

  • 当他们填写配置文件时, completedProfile 将切换为 true,呈现公告。停止使我可以在完成时保存所有答案,并且在他们执行可选操作时不会重定向。
  • 这是我推荐的。你会接受我的回答吗?或者你想让我改进它吗?另外:如果您创建 gist 或 repo(在 github、gitlab 等上),则代码协作会更容易。
猜你喜欢
  • 2014-10-09
  • 2014-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多