Присоединение нескольких схем к Meteor.users
У меня есть 3 схемы, которые используются для одной и той же коллекции Meteor.users. Я пытаюсь присоединить три схемы к коллекции в форме автоформы, однако, когда я иду использовать схему в вспомогательной функции, которую я создал для формы, я получаю неопределенное значение. Можно ли добавить несколько схем для разных типов пользователей в Meteor.users?
Схема администраторов
import SimpleSchema from 'simpl-schema';
import { Tracker } from 'meteor/tracker';
SimpleSchema.extendOptions(['autoform']);
Schema = {};
Schema.AdminProfile = new SimpleSchema({
username: {
type: String,
label: false,
optional: false
},
firstName: {
type: String,
regEx: /^[a-zA-Z-]{2,25}$/,
optional: false,
label: false
},
lastName: {
type: String,
regEx: /^[a-zA-Z]{2,25}$/,
optional: false,
label: false
}
});
Schema.Admin = new SimpleSchema({
emails: {
type: Array,
optional: true
},
"emails.$": {
type: Object
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email,
label: false
},
"emails.$.verified": {
type: Boolean,
optional: true,
},
createdAt: {
type: Date,
autoform: {
type: 'hidden',
label: false
},
autoValue: function(){
return new Date;
}
},
profile: {
type: Schema.AdminProfile,
optional: true
},
services: {
type: Object,
optional: true,
blackbox: true
},
roles: {
type: String,
optional: true,
blackbox: true,
allowedValues: ['courrier', 'buyer', 'admin']
}
}, { tracker: Tracker });
Meteor.users.attachSchema(Schema.Admin);
Схема клиентов
import SimpleSchema from 'simpl-schema';
import { Tracker } from 'meteor/tracker';
SimpleSchema.extendOptions(['autoform']);
SimpleSchema.setDefaultMessages({
messages: {
en: {
passwordMatch: 'Passwords do not match',
},
},
});
Schema = {};
Schema.CustomerProfile = new SimpleSchema({
username: {
type: String,
label: "Username",
autoform: {
label: false
}
},
firstName: {
type: String,
regEx: /^[a-zA-Z-]{2,25}$/,
label: "First Name",
autoform: {
label: false
}
},
lastName: {
type: String,
regEx: /^[a-zA-Z]{2,25}$/,
label: "Last Name",
autoform: {
label: false
}
},
phoneNumber: {
type: String,
label: "Phone Number",
autoform: {
label: false
}
}
});
Schema.Customer = new SimpleSchema({
emails: {
type: Array
},
"emails.$": {
type: Object
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email,
label: 'Email',
autoform: {
label: false
}
},
"emails.$.verified": {
type: Boolean,
optional: true,
},
password: {
type: String,
label: "Password",
min: 8,
autoform: {
label: false
}
},
passwordConfirmation: {
type: String,
label: "Password Confirmation",
min: 8,
custom: function () {
if (this.value !== this.field('password').value) {
return "passwordMatch";
}
},
autoform: {
label: false
}
},
createdAt: {
type: Date,
autoform: {
type: 'hidden',
label: false
},
autoValue: function(){
return new Date;
}
},
profile: {
type: Schema.CustomerProfile
},
services: {
type: Object,
optional: true,
blackbox: true
},
roles: {
type: String,
optional: true,
blackbox: true,
allowedValues: ['courrier', 'buyer', 'admin']
}
}, { tracker: Tracker });
Meteor.users.attachSchema(Schema.Customer);
Новая форма администратора
<template name='new_admin'>
<div class="row">
<div class="col-md-12">
<h1> New User </h1>
</div>
</div>
{{#autoForm collection="Meteor.users" schema=adminSchema id="adminForm" validation="keyup"}}
<div class="row">
<div class="col-md-6">
<div class="field">
{{> afQuickField name='profile.firstName' placeholder='First Name'}}
</div>
</div>
<div class="col-md-6">
<div class="field">
{{> afQuickField name='profile.lastName' placeholder='Last Name'}}
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="field">
{{> afQuickField name='emails.0.address' placeholder='Email Address' value=inviteEmail}}
</div>
</div>
<div class="col-md-6">
<div class="field">
{{> afQuickField name='profile.username' placeholder='Username'}}
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="field">
{{> afQuickField name='profile.phoneNumber' placeholder='Phone Number'}}
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" name="" id="send-invite" class="btn btn-primary">
</div>
</div>
{{/autoForm}}
</template>
Новый админ JS
import './new.html';
import { Admins } from '/imports/api/admins/admins.js';
Template.new_admin.helpers({
inviteEmail: function () {
return FlowRouter.getQueryParam("email");
},
adminSchema: function (){
console.log(Schema.Admin);
return Schema.Admin;
}
});
AutoForm.addHooks('adminForm', {
onSubmit: function (insertDoc, updateDoc, currentDoc) {
this.event.preventDefault();
Meteor.call('inviteAdmin', insertDoc);
},
onError: function (name, error, template) {
console.log(name + " error:", error);
}
});
Форма нового клиента
<template name='new_customer'>
{{> navbar }}
<div id="wrapper">
<div class="white-card">
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 class="signup-driver-h2"> Signup Now </h2>
<div class="circle icon-div"><i class="fa fa-shopping-cart" aria-hidden="true"></i></div>
</div>
</div>
{{# autoForm collection='Meteor.users' schema=customerSchema id='customerForm'validation='keyup'}}
<div class="row">
<div class="col-md-6">
{{> afQuickField name='profile.firstName' placeholder='First Name' class='form-input'}}
</div>
<div class="col-md-6">
{{> afQuickField name='profile.lastName' placeholder='Last Name' class='form-input'}}
</div>
</div>
<div class="row">
<div class="col-md-6">
{{> afQuickField name='password' placeholder='Password' type="password" class='form-input'}}
</div>
<div class="col-md-6">
{{> afQuickField name='passwordConfirmation' type="password" placeholder='Password Confirmation' class='form-input'}}
</div>
</div>
<div class="row">
<div class="col-md-12">
{{> afQuickField name='emails.0.address' placeholder='Email Address' class='form-input'}}
</div>
</div>
<div class="row">
<div class="col-md-6">
{{> afQuickField name='profile.username' placeholder='Username' class='form-input'}}
</div>
<div class="col-md-6">
{{> afQuickField name='profile.phoneNumber' placeholder='Phone Number' class='form-input'}}
</div>
</div>
<div class="row">
<input type="submit" class="btn btn-default signup-submit" value="Become a Customer">
</div>
{{/autoForm}}
</div>
</div>
</div>
</template>
Новый клиент JS
import './new.html';
import { Customers } from '/imports/api/customers/customers.js';
Template.new_customer.helpers({
customerSchema: function(){
console.log(Schema.Customer);
return Schema.Customer;
}
});
AutoForm.addHooks('customerForm', {
onSubmit: function (insertDoc, updateDoc, currentDoc) {
this.event.preventDefault();
Meteor.call('signupCustomer', insertDoc);
},
onError: function (name, error, template) {
console.log(name + " error:", error);
}
});