Javascript iron路由器未等待meteor.user()返回
已解决,但需要改进,请参见答案Javascript iron路由器未等待meteor.user()返回,javascript,meteor,iron-router,Javascript,Meteor,Iron Router,已解决,但需要改进,请参见答案 function completedProfile() { if (Meteor.user() && Meteor.user().profile.primary_email && Meteor.user().profile.display_name && Meteor.user().university_id) { return true; } else
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(在填充字段后挂起启动):
尝试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()。错误是异步函数回调中的异常:TypeError:无法读取未定义的属性“profile”
看起来您正在重定向到名为splash
的路由。你没有包括它,但我假设它存在。一旦重定向,您就完成了登陆
路线的任何逻辑,因此completedProfile()
不会在用户登录后重新运行
if (this.ready() && completedProfile()) {
this.render('bulletin')
} else {
this.render('splash');
}
请尝试以下操作:在landing
路径上停留,但render()
启动页面,直到用户登录
if (this.ready() && completedProfile()) {
this.render('bulletin')
} else {
this.render('splash');
}
我不认为this.subscribe('users').wait()代码>帮助。我会把它拿走。我想您也应该使用action
而不是onBeforeAction
,因为您自己正在调用render()
另见:。听起来似乎适合您的用例。当前的工作解决方案:
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')
}
}
}
}
});
如果我做了If(就绪&完成)重定向(公告)else呈现(启动)
它仍然挂在启动页面上(这一切都在进行中)。我现在来看看即时登录。你确定是stop()
让它工作的吗?我认为将redirect()
更改为render()
是它工作的原因,因为您停留在着陆
路线上,它会做出反应(重新渲染)。如果我在用户完成配置文件字段后立即删除stop,它将重定向到公告,无论是反应性的还是非反应性的。我希望他们点击启动页面上的“完成/继续”按钮,以便查看信息。console.log(Meteor.user().profile)当Meteor.user()
返回未定义的(您正在尝试读取未定义的的属性'profile'
)时,code>抛出错误。尝试if(Meteor.user()){console.log(Meteor.user().profile)}
当他们填写配置文件时,完成的配置文件将切换为true,呈现公告。停止使我可以在完成时保存所有答案,并且在他们做可选内容时不重定向。这是我的建议。你会接受我的回答吗?或者你想让我改进一下?另外:如果您创建一个或repo(在github、gitlab等上),那么在代码上进行协作会更容易。