Search code examples
javascriptmongodbmeteormeteor-accountsmeteor-collection2

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


So, I have just started a meteor project and have included the accounts-password package. The package only supports few keys. I want to add a new SimpleSchema to the users collection with some more fields.

I am not given to create another instance of users collection with

@users = Mongo.Collection('users');
//Error: A method named '/users/insert' is already defined

I can attach a schema but will be forced to keep alot of fields optional, and may not be able to register with the default package otherwise.

Can I add simpleSchema without making other fields optional and still be able to login properly?

Or is there any other work around for this case?

Thank you for help in advance


Solution

  • You can get users collection with:

    @users = Meteor.users;
    

    You can find nice example of defining user collection in the docs of collection2 package: 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
        }
    });