2016-04-28 29 views
0

我想在我第一次將我的流星應用程序加載到計算機上時創建一些虛擬帳戶。這是爲了發展目的。我有這個工作,但我不知道如何將數據添加到子模式。第一次啓動時創建一個完整的用戶配置文件

DB

"profile": { 
    "firstName": "first", 
    "familyName": "last", 
    "phone": "041111111", 
    "address": { 
     "street": "22 Test St", 
     "suburb": "Melbourne", 
    } 
    } 

工作版本,缺少信息

// user creation 
    _.each(users, function(userData) { 
     // return id for use in roles assignment below 
     var userId = Accounts.createUser({ 
     email: userData.email, 
     password: 'password', 
     profile: { 
      firstName: userData.firstName, 
      familyName: userData.familyName, 
      phone: '041111111', 
     }, 
     }); 

不工作 - 我想補充的用戶地址

// user creation 
    _.each(users, function(userData) { 
     // return id for use in roles assignment below 
     var userId = Accounts.createUser({ 
     email: userData.email, 
     password: 'Zaq12wsx', 
     profile: { 
      firstName: userData.firstName, 
      familyName: userData.familyName, 
      phone: '0416089930', 
      address: { 
      street: '22 Test St', 
      suburb: 'Melbourne' 
      } 
     }, 
     }); 

SimpleSchema

Schema.UserProfile = new SimpleSchema({ 
    firstName: { 
     type: String, 
     optional: false, 
    }, 
    familyName: { 
     type: String, 
     optional: false, 
    }, 
    address: { 
     type: Schema.Address, 
     optional: true 
    } 
}); 

Schema.Address = new SimpleSchema({ 
    street: { 
     type: String, 
     optional: false, 
    }, 
    suburb: { 
     type: String, 
     optional: false, 
    }, 
}); 
+0

難道你爲用戶定義任何模式。使用簡單的收集或收集2? –

+0

是的,我正在使用collection2 – bp123

+0

你能不能告訴我,以及。 –

回答

0

根據你的評論,你必須設計爲用戶帶來錯誤的架構。這就是您可以如何爲用戶設計架構。

架構爲用戶

Schema.User = new SimpleSchema({ 
    username: { 
     type: String, 
     optional: true 
    }, 
    emails: { 
     type: Array, 
     optional: true 
    }, 
    "emails.$": { 
     type: Object 
    }, 
    "emails.$.address": { 
     type: String, 
     regEx: SimpleSchema.RegEx.Email 
    }, 
    "emails.$.verified": { 
     type: Boolean 
    } 
    profile: { 
     type: Schema.UserProfile, 
     optional: true 
    }, 
    services: { 
     type: Object, 
     optional: true, 
     blackbox: true 
    }, 
}); 

架構爲用戶配置文件

Schema.UserProfile = new SimpleSchema({ 
    firstName: { 
     type: String, 
     optional: false, 
    }, 
    familyName: { 
     type: String, 
     optional: false, 
    }, 
    address: { 
     type: Schema.Address, 
     optional: true 
    } 
}); 

架構爲用戶配置文件的地址。

Schema.Address = new SimpleSchema({ 
    street: { 
     type: String, 
     optional: false, 
    }, 
    suburb: { 
     type: String, 
     optional: false, 
    }, 
}); 

然後你就可以在用戶模式添加到您的meteor.users這樣

Meteor.users.attachSchema(Schema.User);

您還可以定義在單個對象的架構像這樣

Schema.User = new SimpleSchema({ 
    username: { 
     type: String, 
     optional: true 
    }, 
    emails: { 
     type: Array, 
     optional: true 
    }, 
    "emails.$": { 
     type: Object 
    }, 
    "emails.$.address": { 
     type: String, 
     regEx: SimpleSchema.RegEx.Email 
    }, 
    "emails.$.verified": { 
     type: Boolean 
    } 
    profile: { 
     type: Object, 
     optional: true 
    }, 
    'profile.firstName': { 
     type: String, 
     optional: false, 
    }, 
    'profile.familyName': { 
     type: String, 
     optional: false, 
    }, 
    'profile.address': { 
     type: Object, 
     optional: true 
    }, 
    'profile.address.street': { 
     type: String, 
     optional: false, 
    }, 
    'profile.address.suburb': { 
     type: String, 
     optional: false, 
    }, 
    services: { 
     type: Object, 
     optional: true, 
     blackbox: true 
    }, 
}); 
+0

我不知道我按照你的建議'Meteor.users.attachSchema(Schema.User);'你能告訴我我將如何使用它來添加街道和郊區? – bp123

相關問題