在 Meteor 中注册的问题 [英] Problems with signup in Meteor

查看:54
本文介绍了在 Meteor 中注册的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用普通代码注册 Meteor,尤其是在客户端.正常工作的代码,这意味着无论何时用户创建它都会将用户与会话一起重定向到个人资料页面

I used normal codes, to signup with Meteor, especially on the client side. Normal code that works, meaning that whenever user created it would redirect the user to a profile page together with a session

有效的代码

Template.signup.events({
   'submit form': function(event) {
      event.preventDefault();

      var email = event.target.email.value;
      var username = event.target.username.value;
      var password = event.target.password.value;

      Accounts.createUser({
        email: email,
        username: username,
        password: username;
      });

      Router.go('/profile');
    }
});

上面的代码运行完美,它重定向并显示用户的数据,但我读了发现流星书,它说最好将逻辑放在服务器端,因为我打算添加角色 https://github.com/alanning/meteor-roles

The codes above work perfectly, it redirects and show the user's data, but I read discover meteor book, it says that its better to put the logic on the serverside, because Im planning to add roles https://github.com/alanning/meteor-roles

这是新代码

客户端/signup.js

client/signup.js

Template.signup.events({
    'submit form': function(event) {
      event.preventDefault();

      var signupData = {
        email: event.target.email.value,
        username: event.target.username.value,
        password: event.target.password.value,
        roles: ['customer']
      }

      Meteor.call('signup', signupData, function(error, result) {
        if (error) return alert(error.reason);
        // End
        if (result) {
          Router.go('/profile');
        }
      });
    }
  });

server/collections/user.js

server/collections/user.js

Meteor.methods({
  signup: function(data) {

    id = Accounts.createUser({
      email: data.email,
      password: data.password,
      username: data.username,
    });

    if (data.roles.length > 0) {
      // Need _id of existing user record so this call must come
      // after `Accounts.createUser` or `Accounts.onCreate`
      Roles.addUsersToRoles(id, data.roles, 'default-group');
    }

    return data;
  }
});

下面的模板是 Router.go 要去的地方.

The template below, is where Router.go will go to.

client/templates/accounts/profile.html

client/templates/accounts/profile.html

<template name="profile">
  {{#if currentUser}}

    <h3>Hello: {{ user.username }}</h3>
    <h4>Email: {{ user.email }}</h4>
  {{/if}}
</template>

client/templates/accounts/profile.js

client/templates/accounts/profile.js

Template.profile.helpers({
  user: function() {

    return {
      email: Meteor.user().emails[0].address,
      username: Meteor.user().username
    }
  }
});

不知何故,上面的代码没有返回错误,但现在的问题是它没有在配置文件 html 上显示用户的对象,但它确实将用户的数据保存到了 mongodb 数据库?

For somehow the code above return no error, but the problem right now is that it doesn't show the user's object on profile html, but it did save the user's data to mongodb database?

所以只想澄清(我的逻辑):

So just want to clarify (My logic):

First I click signup -> will do Meteor.call -> then return, if there is no error --> Router.go

推荐答案

  1. 使用客户端中的Accounts.createUser(options),将data.roles变量传入options.profile 键.
  2. 服务器上,使用Accounts.onCreateUser():
  1. Use Accounts.createUser(options) from the client passing in your data.roles variable into the options.profile key.
  2. On the server, use Accounts.onCreateUser():

服务器:

Accounts.onCreateUser(function(options,user){
  if ( options.profile.length > 0) {
    Roles.addUsersToRoles(user._id, options.profile, 'default-group');
  }
  return user;
});

文档

这篇关于在 Meteor 中注册的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆