简体   繁体   English

Meteor.user()始终为null

[英]Meteor.user() always null

I'm a newbie JS developer and I'm trying to develop a Meteor app. 我是一个新手JS开发人员,我正在尝试开发一个Meteor应用程序。 I'm actually trying to use the Account package of Meteor. 我实际上是在尝试使用Meteor的Account包。 I've followed this tuto which is pretty clear : http://blog.benmcmahen.com/post/41741539120/building-a-customized-accounts-ui-for-meteor 我跟着这个非常清楚的tuto: http ://blog.benmcmahen.com/post/41741539120/building-a-customized-accounts-ui-for-meteor

Here is my login page (in Jade) : 这是我的登录页面(在Jade中):

template(name='login')
  div.container
    +menuLogin

    +form_signin


template(name="form_signin")
  form.form-signin(role="form" method="post" action="" id="form-signin" data-toggle="validator")
    h2.form-signin-heading Connexion
    input.form-control(type="text" placeholder="Nom d'utilisateur" name="userName" autofocus="" required )
    input.form-control(type="password" placeholder="Mot de passe" name="password" required )

    +profil_choice

    button.btn.btn-primary.btn-lg.btn-block.btn-connection(type="submit") Connexion
    div.alert.alert-danger.alert-dississible(id="alert-login" role="alert") 
        button.close(type="button" data-dismiss="alert")
            span(aria-hidden="true") ×
            span.sr-only Close
        p(id="alert-login-text")

And JavaScript : 和JavaScript:

Profils = new Mongo.Collection('profils');
Meteor.subscribe('profils');
Meteor.subscribe('users');

var userCursor = Meteor.users.find();
var handle = userCursor.observe({
  changed: function (tmp){
    console.log(tmp);
    Router.go('start');
  }
})

var trimInput = function(val){
  return val.replace(/^\s*|\s*$/g, "");
}

Meteor.autorun(function (){
  var message = Session.get('errorMessage');
  if (message){
    $('#alert-login-text').text(message);
    $('#alert-login').show(200).delay(3000).hide(200);
    Session.set('errorMessage', null);
  }
});

var checkLoginParams = function(userName, password, profil){
  if (profil == ""){
    Session.set('errorMessage', "Veuillez selectionner un profil !");
    return false;
  }
  return true;
}

Template.form_signin.events({
  'submit form': function (event, template){
    event.preventDefault();

    var userName = template.find("input[name=userName]").value;
    var password = template.find("input[name=password]").value;
    var profil = template.find("input[name=profilName]").value;

    var username = trimInput(userName);
    console.log("user : " + username + "  password : " + password);

    if (checkLoginParams(username, password, profil) == false){
      return;
    }

    Meteor.loginWithPassword(username, password, function (error){           
      if (error){
        Session.set('errorMessage', "Nom d'utilisateur ou mot de passe incorrect");
        console.log(error.reason);
      }
    });
    return false;
  }
});

I'm actually changing the template (with Iron router) when the Object user is changed on the database, because I couldn't find anything else to notice me wehther or not i'm logged in. 我实际上在数据库上更改了Object用户时更改了模板(使用Iron路由器),因为我找不到任何其他的注意我,无论我是否登录。

Router.route('login', function (){
  this.render('login');
});

Router.route('start',{
  path: 'start',
  onBeforeAction: function () {
    if (Meteor.user()){
      this.render('start');
    }
    else{
      if (Meteor.loggingIn()){
        this.render('start');
      }
      else{
        Router.go('login');
      }
    }
  }
});

The problem is, no matter where I call Meteor.user(), it's always null. 问题是,无论我在哪里调用Meteor.user(),它总是为空。 So... I am apparently not connected. 所以......我显然没有联系。

There is only one user in the database and I'm sure that the connection parameters are good. 数据库中只有一个用户,我确信连接参数很好。

Moreover, why the hell can't I do a Router.go('start') in the loginWithPassword() callback ? 而且,为什么我不能在loginWithPassword()回调中做一个Router.go('start')?

EDIT : Here is the creation of my user on the server side 编辑:这是在服务器端创建我的用户

if (Meteor.isServer) {
  Meteor.startup(function() 
  {
    Accounts.createUser({
        username : "admin",
        password : "123",
        email : "test@test.fr",
        isAdmin : true
  });
});

The problem was resolved by updating my version of Meteor. 通过更新我的Meteor版本解决了这个问题。 Actually... I don't really understand what was wrong. 其实......我真的不明白出了什么问题。

You don't create user, so you can't login to account that doesn't exist http://docs.meteor.com/#/full/accounts_createuser 您不创建用户,因此您无法登录不存在的帐户http://docs.meteor.com/#/full/accounts_createuser

You could create callback for login like: 您可以为登录创建回调,例如:

if (error)
        {
            console.log(error.reason);
        }

and erorr that user doesn't exist would pop-up 弹出用户不存在的erorr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM