2015-12-08 85 views
1

我有一個攔截器:爲什麼Meteor.user和Meteor.userId有所不同?

Router.onBeforeAction(function() { 
    if (!Meteor.userId()) { 
     console.log("lets login"); 
     Router.go("login"); 
    } else { 
     this.next(); 
    } 
}, { 
    except: ['login', 'signup'] 
}); 

它工作得很好,直到我更換Meteor.userId()用於Meteor.user(!)。看起來,.user刷新頁面時未定義並將其重定向到我的登錄頁面。我的登錄路由器也驗證.user和這裏是正確的。

爲什麼這種差異?

+1

請參閱我對[此問題]的回答(https://stackoverflow.com/questions/32386208/meteor-logout-causes-helper-to-rerun)。 –

+1

Meteor.userId()返回當前用戶ID和Meteor.user()返回整個對象 – Nakib

+0

@Nakib我知道,但這不是問題。我的if!Meteor.user()也驗證對象,但刷新後它總是未定義。 –

回答

0

這是真的,Meteor.userId()返回的是id,而Meteor.user()返回該對象。

然而,返回一個對象(Meteor.user())需要更多的時間不僅僅是返回一個ID,由於異步問題,通過腳本檢查如果時間(!Meteor.user()) {...}Meteor.user()尚未處理並返回!

其結果是,有幾種方法,由於與異步的問題(例如,在流星/陣營的應用,我們可以有像訂閱,並等待手柄準備

相關問題