gpt4 book ai didi

javascript - 铁路由器不等待 meteor.user() 返回

转载 作者:行者123 更新时间:2023-11-30 12:33:55 25 4
gpt4 key购买 nike

已解决但需要改进,请查看答案

    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 语句中的所有字段均为 null)。

尝试 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() 返回。错误是 异步函数回调中的异常:类型错误:无法读取未定义的属性“配置文件”
.

最佳答案

看起来您正在重定向到名为 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将适用于您的用例。

关于javascript - 铁路由器不等待 meteor.user() 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26726418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com