我有一个拦截器:
Router.onBeforeAction(function() {
if (!Meteor.userId()) {
console.log("lets login");
Router.go("login");
} else {
this.next();
}
}, {
except: ['login', 'signup']
});
它非常好用,直到我为Meteor.user()替换了!Meteor.userId()。似乎.user刷新页面时未定义并将其重定向到我的登录页面。我的登录路由器也验证.user,这是正确的。
为什么会出现这种差异?
答案 0 :(得分:0)
这是真的,Meteor.userId()返回id,而Meteor.user()返回该对象。
然而,由于异步问题,返回对象( Meteor.user())所花费的时间比返回ID要花费更多时间,直到脚本检查 if(! Meteor.user()){...} , Meteor.user()尚未处理并返回!
因此,有几种方法可以解决这个异步问题(例如,在Meteor / React应用程序中,我们可能会有类似 subscribe 的东西,并等到 handle < / em> 准备好)