[英]iron-router not waiting on meteor.user() to return
解決但尋找改進,看到答案
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語句中的所有字段均為空),頁面將重定向到我的splash模板。
嘗試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
。
看起來您正在重定向到名為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 。 聽起來像即時登錄將適合您的用例。
當前工作方案:
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')
}
}
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.