【发布时间】: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