2014-03-06 44 views
2

内未定义回报我在流星以下设置:Meteor.user()路由器

当用户点击URL根和未登录,我展示一个欢迎页面。

当用户点击根URL并登录时,我想重定向到用户的“东西”页面。

问题是,Meteor.user()在路由器内部是未定义的。

这种结构的正确方法是什么?

<body> 
    {{# if currentUser}} 
     {{> thing}} 
    {{else}} 
     {{> welcome}} 
    {{/if}} 
</body> 

var MyRouter = Backbone.Router.extend({ 
    routes: { 
     "": "welcome", 
     "/things/:thingId": "thing" 
    }, 
    welcome: function() { 
     var user = Meteor.user(); 
     console.log(user); //undefined 
     // Redirect to user's thing 
    }, 
    thing: function(thingId) { 
     Session.set("currentThingId", thingId); 
    } 
}); 

回答

0

您确定您当前的用户已经注册吗? Meteor.user()的调用在双方(客户端和服务器)都可用,因此您应该有权访问路由器文件中的当前用户实例。比如我测试,如果当前用户是我的路由器中登录的是这样的:在服务器端

var requireLogin = function() { 
    if (! Meteor.user()) { 
     if (Meteor.loggingIn()) 
      this.render(this.loadingTemplate); 
     else 
      this.render('accessDenied'); 
      this.stop(); 
     } 
} 

Router.before(requireLogin, {only: 'postSubmit'}) 

检查您蒙戈DB:在客户端

$ meteor mongo 
$ >db.users.find().count() // Should be greather than 0 

控制。例如,打开一个Chrome控制台,只需输入:

Meteor.user(); 
null // Means user is currently not logged in 
    // otherwise you should receive a JSON object 
+0

嗨,是的,用户是绝对注册并登录(在Chrome控制台中确认)。 –