流星:对创建的用户显示警报

meteor: show alert on created user

我想向用户显示如下消息:"I have sent a email to activate your account"。

我不能,因为创建用户后我还没有找到它的挂钩。

你知道这样做的方法吗?

我目前永久显示一条消息,但我不想这样。我只想在用户被点赞时显示一次。

这里有 2 个选项,如果您在客户端创建用户,只需使用

Accounts.createUser({email: email, password: password}, function(err) {
 if(!err){
   alert(""I have sent a email to activate your account")
  }
});

或者,如果您从方法创建用户,它应该如下所示。

//Server.js
    Meter.method({
     createUser:function(username,email,password){
       //create user logic.
      }
    })

在客户端上应该是这样的。

Meteor.call('createUser',username,email,password,function(err,result){
  if(!err){
   alert(""I have sent a email to activate your account")
  }
});

在这两种情况下,我们都使用了一个额外的参数,名为 callback 这个函数,接受另外两个参数 err,result,所以如果创建帐户时没有错误,警报应该被触发

您应该能够在客户端的 createUser() 回调中添加警报。假设您提交了类似表单的内容来创建用户,那么您会做 ...

Template.myTemplate.events({
  'submit #new-user-form': function(event) { 
    // These next few lines will depend on what your template looks like and what data you require on login
    // Here I've assumed just username and pw in appropriately-named inputs on your page
    var email = $('#email-input').val();
    var password = $('#password-input').val();
    event.preventDefault();

    Accounts.createUser({email: email, password: password}, function(error) { 
      if (error) { alert("There was an error!"); }
      else { alert("I have sent you an email to verify your account"); }
    });
  }
});     

如果您想通过安装帐户-ui 来完成此行为,the documentation 不会显示任何您可以连接的内容。但是,您可以像这样手动完成:

Template.myTemplate.events({
  'click .login-form-create-account #login-buttons-password': function() { 
    alert("I sent you an email to verify your account");
  }
});     

不幸的是,即使用户没有成功创建,它仍然会触发。