javascript - Meteor 中的简单架构错误

标签 javascript meteor schema accounts

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

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 中定义了地址架构中的所有字段。因此,我不知道为什么当我在表单中输入所有信息时,出现“需要地址”错误。有谁知道发生了什么事吗?预先感谢您!

最佳答案

看来您正在使用accounts-entry 包。您可以在此文件中看到创建帐户的代码:

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

第 29 行如下所示:

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

如果您在调用扩展方法后调试并查看 profile 的值,您将看到以下内容:

{
    "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、城市、州、zipCode)移至 Schemas.UserProfile 架构中,它将起作用。

关于javascript - Meteor 中的简单架构错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29546424/

相关文章:

javascript - request.settimeout nodejs 似乎不起作用?

java - 具有多个模式的 WSDL 的自定义解析和打印方法

hadoop - 在Pig中获取不同的元组集

database-design - Mnesia 中 set 和ordered_set 之间的区别?

javascript - 如何查找表格列中数字的出现次数?

javascript - 如何将某些样式应用于 Angular Material 表中的表行

javascript - 当我长时间滑过最后一张幻灯片时,Swiperjs 重置到上一张幻灯片

javascript - meteor.js - 将数组推送到用户集合

javascript - Meteor JS : How do I insert a document into a collection, 但仅在客户端?

node.js - Jasmine Node - 包括助手