0

伙计们,

我需要帮助。我已经有一段时间了,似乎无法弄清楚。应该很简单。我正在使用 aldeed collection2,但我似乎无法通过创建用户帐户方法引发的验证错误。我的模式非常标准,附加到meteor.users 集合中:

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);

我的服务器上创建用户的方法如下:

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;
});

对于我的客户,我有以下 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('/');
      }
    });

  }

有谁知道我做错了什么?架构未验证电子邮件地址。我收到错误: 架构不允许 email.0.address

4

1 回答 1

1

您正在创建一个对象:

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

关键是文字字符串'email.$.address'

所以当你这样做时:

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

email找不到钥匙,因为userdata.emails[0]没有emails钥匙。相反,关键是'email.$.address'. 此外,该模式没有名为email.$.address. 它有一个叫emails.$.address

尝试:

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('/');
      }
    });

  }

然后

//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;
  }
});
于 2015-06-19T20:44:30.917 回答