Meteor:Accounts.createUser()不会创建用户

时间:2015-11-20 09:29:27

标签: javascript meteor meteor-accounts meteor-helper meteor-publications

我正在构建一个人们无法自己创建帐户的应用程序,但创建的第一个用户(我)可以在应用程序中的表单中创建用户。

这就是我在/lib/config/account.js中设置的原因:forbidClientAccountCreation: true,

我的问题是,当我登录时,我无法在我的表单中创建用户..(即使我将此选项设置为false也是如此。 这是我的代码:

userAdd.js:

Template.userAdd.events({
'submit .new-user': function(event) {
    event.preventDefault();

    var email = $('input[name="email"]').val();;
    var username = $('input[name="username"]').val();;

    Accounts.createUser({
        email: email,
        username: username,
        password: 'toto',
        profile: {

        }
    });

    event.target.email.value = "";
    event.target.username.value = "";
}
});

/server/publications/user.js:

 Meteor.publish('users', function() {
   return Meteor.users.find();
 })

 Meteor.users.allow({
   'insert': function (userId, doc) {
     return true; 
   }
 });

我的userList,其中显示用户列表:

Meteor.subscribe('users');

Template.usersList.helpers({
   users: function() {
     return Meteor.users.find();
   }
});

我也尝试使用Meteor.users.insert(),但它也不起作用..

你能帮我吗?

添加了路线:

FlowRouter.route('/users', {
name: "users",
 action: function() {
    BlazeLayout.render('applicationLayout', {
        menu: 'menu',
        container: 'usersList'
    });
 }
});

AccountsTemplates.configureRoute('changePwd');
AccountsTemplates.configureRoute('forgotPwd');
AccountsTemplates.configureRoute('resetPwd');
AccountsTemplates.configureRoute('signIn');

AddUser.html

<template name="userAdd">
  <form class="ui form new-user">
    <div class="inline fields">
        <div class="seven wide field">
            <label>Email</label>
            <input type="text" name="email" placeholder="Email">
        </div>
        <div class="seven wide field">
            <label>Username</label>
            <input type="text" name="username" placeholder="Username">
        </div>
        <button class="ui button icon right labeled teal" type="submit" name="submit"><i class="right checkmark icon"></i>Valider</button>
    </div>
  </form>
</template>

user.js的

Template.tableUser.helpers({
  email: function() {
    return this.emails[0].address; 
  }
});

.Packages

insecure                # Allow all DB writes from clients (for prototyping)
kadira:flow-router
semantic:ui
flemay:less-autoprefixer
underscore
kadira:blaze-layout
arillo:flow-router-helpers
zimme:active-route
aldeed:collection2
accounts-password
useraccounts:flow-routing
useraccounts:semantic-ui
accounts-base

这些都与账户有关......

1 个答案:

答案 0 :(得分:2)

谢谢@JeremyK和@Sasikanth! 它现在正在工作,

我将代码更改为服务器端。不知道一切是否完美,但这是工作代码:

addUser.js:

Meteor.subscribe('users');

Template.userAdd.events({
 'submit .new-user': function(event) {
    event.preventDefault();

    var email = $('input[name="email"]').val();
    var password = $('input[name="password"]').val();
    var firstname = $('input[name="firstname"]').val();
    var lastname = $('input[name="lastname"]').val();

    Meteor.call("createUsers", email, password, firstname, lastname);

    event.target.email.value = "";
    event.target.password.value = "";
    event.target.firstname.value = "";
    event.target.lastname.value = "";
 }
});

/server/methods/user.js

Meteor.methods({
 createUsers: function(email, password, firstname, lastname) {
    Accounts.createUser({
        password: password,
        username: firstname + ' ' + lastname,
        email: email,
        createdAt: new Date(),
    });
 },

 deleteUser : function(id){
    return Meteor.users.remove(id);
 },
});

user.js的

Template.tableUser.events({
  "click .delete": function () {
    var idUser= this._id;
    Meteor.call('deleteUser',{_id:idUser})
  }
});

非常感谢:)