繁体   English   中英

如何使用meteorjs中的accounts-password包向用户集合添加collection2架构?

[英]How to add a collection2 schema to users collection using accounts-password package in meteorjs?

所以,我刚刚开始了一个流星项目,并且已经包含了帐户密码包。 该软件包仅支持少量密钥。 我想将一个新的SimpleSchema添加到带有更多字段的users集合中。

我不会创建另一个用户集合实例

@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的存在someField重要,它可以初始化为默认对象(空数组, null ,0 ...)? 然后一个默认值适合,当Collection2清理文档时,它将被添加。
以上都不是? 可选的。


作为一个有点个人的说明,我更喜欢这种代码:

someUser.friends.forEach(sendGifts);

对于这种:

if(someUser.hasOwnProperty('friends')) {//Or _.has(someUser, 'friends') but it sounds sad
  someUser.friends.forEach(sendGifts);
}

在第二个代码中, friends是一个可选字段,因此我们不确定它是否存在或未定义。 undefined上调用forEach产生一个很大的错误,因此我们必须首先检查字段是否存在...因此,我建议稍微避免使用可选字段以保持一致性简单性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM