Meteor 中的简单架构错误

Simple Schema error in Meteor

我正在使用 meteor 帐户和简单模式供用户在我的应用程序中注册和输入他们的个人资料信息。我为用户定义了一个模式,并为它附加了一个地址模式。但是,当我去注册一个帐户时,尽管我填写了地址的所有必填字段,但我仍然收到错误 "Address required"。这是我的架构:

Schemas.UserAddress = new SimpleSchema({ //UserAddress schema defined

streetAddress: {
    type: String,
    max: 100,
    optional: false
},
city: {
    type: String,
    max: 50,
    optional: false
},
state: {    
    type: String,
    regEx: /^A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY]$/,
    optional: false
},
zipCode: {
type: String,
regEx: /^[0-9]{5}$/,
optional: false

} });

var Schemas = {};

Schemas.UserProfile = new SimpleSchema({
companyName: {
    type: String,
    regEx: /^[a-zA-Z-]{2,25}$/,
    optional: false
},
tireMarkup: {
    type: Number,
    optional: true,
    min: 1
},
phoneNum: {
    type: String,
    regEx: /^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/,
    optional: false    
},
confirmed: {
    type: Boolean,
    optional: true
},
});


Schemas.User = new SimpleSchema({
emails: {
    type: [Object],
    optional: false
},
"emails.$.address": {
    type: String,
    regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
    type: Boolean
},
createdAt: {
    type: Date
},
profile: {
    type: Schemas.UserProfile,
    optional: false
},
address: { //Attach user address schema
    type: Schemas.UserAddress, 
    optional: false
},
services: {
    type: Object,
    optional: true,
    blackbox: true
}
});

Meteor.users.attachSchema(Schemas.User);

这是我创建注册表单的帐户配置:

Meteor.startup(function () {
AccountsEntry.config({
  logo: '',                  // if set displays logo above sign-in options
  termsUrl: '/terms-of-use',         // if set adds link to terms  'you agree to ...' on sign-up page
  homeRoute: '/',                    // mandatory - path to redirect to after sign-out
  dashboardRoute: '/dashboard/',      // mandatory - path to redirect to after successful sign-in
  profileRoute: '/profile',
  passwordSignupFields: 'EMAIL_ONLY',
  showOtherLoginServices: true,      // Set to false to hide oauth login buttons on the signin/signup pages. Useful if you are using something like accounts-meld or want to oauth for api access
  extraSignUpFields: [

     {
        field: "companyName",                           
        label: "Company Name",                     
        placeholder: "Petrocon",                
        type: "text",                           
        required: true                         
    },
    {
        field: "firstName",                       
        label: "First Name",                    
        placeholder: "John",                
        type: "text",                            
        required: true                           
    },  
    {
        field: "lastName",                          
        label: "Last Name",                      
        placeholder: "Nguyen",                 
        type: "text",                            
        required: true                          
    },
    {   field: "streetAddress",
        label: "Street Address",
        placeholder: "12345 Main Street",
        type: "text",
        required: true
    },
    {   field: "city",
        label: "City",
        placeholder: "Springfield",
        type: "text",
        required: true
    },
    {   field: "state",
        label: "State",
        placeholder: "Missouri",
        type: "text",
        required: true
    },
    {   field: "zipCode",
        label: "Zip Code",
        placeholder: "65801",
        type: "text",
        required: true
    },
    {
        field: "phoneNum",                          
        label: "Contact Number",                     
        placeholder: "316-000-0000",                 
        type: "text",                            
        required: true                           
    }  
    ]
});
});

如您所见,我在 AccountsEntry.config.. 中定义了地址模式中的所有字段。因此我不知道为什么当我在表单中输入所有信息时,出现错误 'address required'。有谁知道发生了什么事?提前致谢!

您似乎在使用 accounts-entry 软件包。您可以在这个文件中看到创建帐户的代码:

https://github.com/Differential/accounts-entry/blob/9aac00cb3c67afcbb1cc990c7af1f2c7607a2337/server/entry.coffee

第 29 行如下所示:

profile: _.extend(profile, user.profile)

如果在调用 extend 方法后调试并查看配置文件的值,您将看到:

{
    "companyName": "Petrocon",
    "firstName": "John",
    "lastName": "Nguyen",
    "streetAddress": "12345 Main Street",
    "city": "Springfield",
    "state": "Missouri",
    "zipCode": "65801",
    "phoneNum": "316-000-0000"
}

您的 Schemas.User 架构没有名为 "address" 的 属性。

可能有一些巧妙的方法可以满足您的需求,但最简单的方法是摆脱 Schemas.UserAddress 模式。将这些属性(streetAddress、city、state、zipCode)移动到 Schemas.UserProfile 架构中,它将起作用。