Meteor-Community-Packages / meteor-collection2

A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
https://packosphere.com/aldeed/collection2
MIT License
1.02k stars 108 forks source link

Password is not allowed by your schema #324

Closed Nisthar closed 6 years ago

Nisthar commented 8 years ago

I am new to meteor and simple schema. I find it very confusing and difficult to use as meteor is slightly different than other frameworks. I am getting "Password is not allowed by the schema" error. I know its because there is no password field in my schema. But I used the code from the collection2 official doc.

my code:

    Template.register.events({
        'submit form': function(event) {
       event.preventDefault();
        console.log("Registering new user....");
       let userDetails = {
        username: event.target.registerUsername.value,
        password: event.target.registerPassword.value,
        emails: [{ address: event.target.registerEmail.value, verified: false }],
        profile: {
            accountType: "free"
        }
    };
    let errors = [];
    let regContext = Meteor.users.simpleSchema().namedContext();
    regContext.addInvalidKeys({ name: "username", type: "required", value: "Please enter your username"});
    if (!regContext.validate(userDetails, { modifier: false })) {
        let fields = regContext.invalidKeys();
        for (let i in fields) {
            errors.push(regContext.keyErrorMessage(fields[i].name));
        }
        console.log(errors);
        Session.set('errors', errors);
     } else {
        Accounts.createUser(userDetails, function(error) {
            if (error) {
                console.log(error.reason);
                errors.push(error.reason);
                Session.set('errors',errors);
            } else {
                sAlert.warning('Registered Successfully');
                FlowRouter.go('home');
            }
        });
    }
  }

});

Template.register.helpers({
 errors: function() {
    if (Session.get('errors')) {
        let errors = Session.get('errors');
        delete Session.get('errors');
        return errors;
    } else {
        return false;
     }
 }
});

My schema: `Schema.User = new SimpleSchema({ username: { type: String, },

emails: {
    type: Array,
},
"emails.$": {
    type: Object
},
"emails.$.address": {
    type: String,
    regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
    type: Boolean
},

createdAt: {
    type: Date,
    optional: true,
    autoValue: function(){
        return new Date;
    }
},
profile: {
    type: Schema.UserProfile,
    optional: true
},
// Make sure this services field is in your schema if you're using any of the accounts packages
services: {
    type: Object,
    optional: true,
    blackbox: true
},

// In order to avoid an 'Exception in setInterval callback' from Meteor
heartbeat: {
    type: Date,
    optional: true
}

});

Meteor.users.attachSchema(Schema.User);`

darkship commented 8 years ago

Hi, First you need to understand that Accounts.createUser isn't an Meteor.users.insert.

Then that your schema is compatible with Meteor.users which doesn't need the same arguments than Accounts.createUser.

Finally if you want to use a schema for Accounts.createUser you need to create a specific schema for it.

Nisthar commented 8 years ago

Ok

On Thu, Apr 28, 2016 at 2:19 PM, darkship notifications@github.com wrote:

Hi, First you need to understand that Accounts.createUser http://docs.meteor.com/#/full/accounts_createuser isn't an Meteor.users.insert.

Then that your schema is compatible with Meteor.users which doesn't need the same arguments than Accounts.createUser.

Finally if you want to use a schema for Accounts.createUser you need to create a specific schema for it.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/aldeed/meteor-collection2/issues/324#issuecomment-215355742

aldeed commented 6 years ago

Closing old issues. Please comment if this is still an issue and should be reopened.