createuser validation error on method with aldeed collection2

574 views Asked by At

Folks,

I need help. I've been at this for sometime now and cant seem to figure it out. It should be simple. I'm using the aldeed collection2 and I cant seem to get past validation error being thrown by the create user account method. My schema is pretty standard, attached to meteor.users collection:

Schema={}
//SimpleSchema.debug = true;


Schema.UserProfile = new SimpleSchema({

  picture: {
    type: String,
    optional: true
  },
  updatedAt: {
    type: Date,
    autoValue: function() {
      if (this.isUpdate) {
        return new Date();
      }
    },
    denyInsert: true,
    optional: true
  },
  roles: {
    type: String,
    optional: true
  }
});

Schema.User = new SimpleSchema({
  _id: {
    type: String,
    regEx: SimpleSchema.RegEx.Id,
    optional: true,
    denyUpdate: true
  },

  emails: {
    type: [Object],
    optional: true
  },
  "emails.$.address": {
    type: String,
    regEx: SimpleSchema.RegEx.Email,
    label: ""

  },
  "emails.$.verified": {
    type: Boolean,
    optional: true
  },
  createdAt: {
    optional: true,
    type: Date,
    autoValue: function() {
      if (this.isInsert) {
        return new Date;
      } else if (this.isUpsert) {
        return {$setOnInsert: new Date};
      } else {
        this.unset();
      }
    }
  },
  profile: {
    type: Schema.UserProfile,
    optional: true
  },
  services: {
    type: Object,
    optional: true,
    blackbox: true
  },

  // Option 2: [String] type
  // If you are sure you will never need to use role groups, then
  // you can specify [String] as the type
  roles: {
    type: [String],
    optional: true,
    autoValue: function() {
      if (this.isInsert) {
        return ['user'];
      } else if (this.isUpsert) {
        return {$setOnInsert: ['user']};
      } else {
        this.unset();
      }
    }
  },

  password: {
    type: String,
    label: "Password",
    min: 6
  }

});

/* Attach schema to Meteor.users collection */
Meteor.users.attachSchema(Schema.User);

The method on my server for creating the user is like below:

Accounts.config({
  forbidClientAccountCreation : true
});

//creates user on server
Meteor.methods({
  createNewUserAccount: function(userdata) {
    var userId;
    check(userdata, Schema.User);
    //console.log(userdata);

    userId = Accounts.createUser({
      email: userdata.emails[0].address,
      password: userdata.password,
      profile: userdata.profile
    });
    //console.log(userId);
    return userId;
  }
});

Accounts.onCreateUser(function(options, userdata) {
  //user.profile = {};
  // we wait for Meteor to create the user before sending an email
  //need to address the exception when existing email is tried for signing up
  Meteor.setTimeout(function () {
    Accounts.sendVerificationEmail(userdata._id);
  }, 2 * 1000);
return userdata;
});

for my client, I have the following signup.js

Template.signup.events({
  'submit form': function(e){
    // Prevent form from submitting.
    e.preventDefault();
    //console.log(doc);
    user = {
      'email.$.address': $('[name="emails.0.address"]').val(),
      password: $('[name="password"]').val()
    };

    Meteor.call('createNewUserAccount', user, function(error) {
      if (error) {
        return alert(error.reason);
      } else {
        Router.go('/');
      }
    });

  }

Does anyone know what I'm doing wrong? The schema does not validate the email address.I get the error: email.0.address is not allowed by the schema

1

There are 1 answers

2
fuzzybabybunny On BEST ANSWER

You're creating an object:

user = {
  'email.$.address': $('[name="emails.0.address"]').val(),
  password: $('[name="password"]').val()
};

The key is the literal string 'email.$.address'

So when you do:

    userId = Accounts.createUser({
      email: userdata.emails[0].address,
      password: userdata.password,
      profile: userdata.profile
    });

The email key can't find userdata.emails[0] because there is no emails key. Instead, the key is 'email.$.address'. Also, the schema does not have a key called email.$.address. It has one called emails.$.address

Try:

Template.signup.events({
  'submit form': function(e){
    // Prevent form from submitting.
    e.preventDefault();
    //console.log(doc);
    user = {
      'emails.$.address': $('[name="emails.0.address"]').val(),
      password: $('[name="password"]').val()
    };

    Meteor.call('createNewUserAccount', user, function(error) {
      if (error) {
        return alert(error.reason);
      } else {
        Router.go('/');
      }
    });

  }

Then

//creates user on server
Meteor.methods({
  createNewUserAccount: function(userdata) {
    var userId;
    check(userdata, Schema.User);
    //console.log(userdata);

    userId = Accounts.createUser({
      email: userdata['emails.$.address'],
      password: userdata.password,
      profile: userdata.profile
    });
    //console.log(userId);
    return userId;
  }
});