如何使用 meteorjs 中的帐户密码包将 collection2 模式添加到用户集合?
How to add a collection2 schema to users collection using accounts-password package in meteorjs?
所以,我刚刚启动了一个 meteor 项目,并包含了 accounts-password 包。该软件包仅支持几个键。我想将一个新的 SimpleSchema 添加到包含更多字段的用户集合中。
我没有使用
创建另一个用户集合实例
@users = Mongo.Collection('users');
//Error: A method named '/users/insert' is already defined
我可以附加一个架构,但将被迫保留很多字段可选,否则可能无法使用默认包注册。
我可以添加 simpleSchema 而不将其他字段设为可选并且仍然能够正确登录吗?
或者对于这种情况还有其他解决方法吗?
提前感谢您的帮助
您可以通过以下方式获取用户集合:
@users = Meteor.users;
您可以在 collection2 包的文档中找到定义用户集合的好例子:https://atmospherejs.com/aldeed/collection2
Schema = {};
Schema.User = new SimpleSchema({
username: {
type: String,
regEx: /^[a-z0-9A-Z_]{3,15}$/
},
emails: {
type: [Object],
// this must be optional if you also use other login services like facebook,
// but if you use only accounts-password, then it can be required
optional: true
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
type: Boolean
},
createdAt: {
type: Date
},
profile: {
type: Schema.UserProfile,
optional: true
},
services: {
type: Object,
optional: true,
blackbox: true
},
// Add `roles` to your schema if you use the meteor-roles package.
// Option 1: Object type
// If you specify that type as Object, you must also specify the
// `Roles.GLOBAL_GROUP` group whenever you add a user to a role.
// Example:
// Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP);
// You can't mix and match adding with and without a group since
// you will fail validation in some cases.
roles: {
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
}
});
您可以通过三种方式来适应将架构附加到此类集合:
- 将每个新字段设为可选。
- 有默认值(例如
friends
默认为[]
)。
- 更新 UI 以包含新的强制性元素("P = NP" 或 "P != NP" 的收音机)。
每个选项本身都有些道理。选择在当前上下文中看起来最合乎逻辑 的内容,以及最不会让您头疼的内容。
当他注册时,您绝对需要 someField
的用户给定值吗?然后你必须更新 UI 来获取这个值。
someField
的存在是否重要,可以初始化为默认对象(空数组,null
, 0...)?然后会适配一个默认值,在Collection2清理文档的时候加上。
None 以上?可选。
作为个人说明,我更喜欢这种代码:
someUser.friends.forEach(sendGifts);
对这种:
if(someUser.hasOwnProperty('friends')) {//Or _.has(someUser, 'friends') but it sounds sad
someUser.friends.forEach(sendGifts);
}
在第二个代码中 friends
是一个可选字段,因此我们不确定它是存在还是未定义。在 undefined
上调用 forEach
会导致一个很大的错误,所以我们必须首先检查字段是否存在...因此,我建议稍微避免 consistency[= 的可选字段46=] 和 简单 .
所以,我刚刚启动了一个 meteor 项目,并包含了 accounts-password 包。该软件包仅支持几个键。我想将一个新的 SimpleSchema 添加到包含更多字段的用户集合中。
我没有使用
创建另一个用户集合实例@users = Mongo.Collection('users');
//Error: A method named '/users/insert' is already defined
我可以附加一个架构,但将被迫保留很多字段可选,否则可能无法使用默认包注册。
我可以添加 simpleSchema 而不将其他字段设为可选并且仍然能够正确登录吗?
或者对于这种情况还有其他解决方法吗?
提前感谢您的帮助
您可以通过以下方式获取用户集合:
@users = Meteor.users;
您可以在 collection2 包的文档中找到定义用户集合的好例子:https://atmospherejs.com/aldeed/collection2
Schema = {};
Schema.User = new SimpleSchema({
username: {
type: String,
regEx: /^[a-z0-9A-Z_]{3,15}$/
},
emails: {
type: [Object],
// this must be optional if you also use other login services like facebook,
// but if you use only accounts-password, then it can be required
optional: true
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
type: Boolean
},
createdAt: {
type: Date
},
profile: {
type: Schema.UserProfile,
optional: true
},
services: {
type: Object,
optional: true,
blackbox: true
},
// Add `roles` to your schema if you use the meteor-roles package.
// Option 1: Object type
// If you specify that type as Object, you must also specify the
// `Roles.GLOBAL_GROUP` group whenever you add a user to a role.
// Example:
// Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP);
// You can't mix and match adding with and without a group since
// you will fail validation in some cases.
roles: {
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
}
});
您可以通过三种方式来适应将架构附加到此类集合:
- 将每个新字段设为可选。
- 有默认值(例如
friends
默认为[]
)。 - 更新 UI 以包含新的强制性元素("P = NP" 或 "P != NP" 的收音机)。
每个选项本身都有些道理。选择在当前上下文中看起来最合乎逻辑 的内容,以及最不会让您头疼的内容。
当他注册时,您绝对需要 someField
的用户给定值吗?然后你必须更新 UI 来获取这个值。
someField
的存在是否重要,可以初始化为默认对象(空数组,null
, 0...)?然后会适配一个默认值,在Collection2清理文档的时候加上。
None 以上?可选。
作为个人说明,我更喜欢这种代码:
someUser.friends.forEach(sendGifts);
对这种:
if(someUser.hasOwnProperty('friends')) {//Or _.has(someUser, 'friends') but it sounds sad
someUser.friends.forEach(sendGifts);
}
在第二个代码中 friends
是一个可选字段,因此我们不确定它是存在还是未定义。在 undefined
上调用 forEach
会导致一个很大的错误,所以我们必须首先检查字段是否存在...因此,我建议稍微避免 consistency[= 的可选字段46=] 和 简单 .