How to add a collection2 schema to a user collection using the password / password package in meteorjs?

So, I just started the meteor project and included a package of account passwords. The package only supports a few keys. I want to add a new SimpleSchema to a multi-field user collection.

I have not been given the opportunity to create another instance of the user collection using

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

I can attach the scheme, but will be forced to store many fields optionally and may not be able to register with the default package.

Can I add simpleSchema without making additional fields optional and can still log in correctly?

Or is there any other job for this case?

Thank you for your help.

+6
source share
2 answers

You can get a collection of users with:

 @users = Meteor.users; 

You can find a good example of defining a user collection in the documents of the 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 } }); 
-1
source

You have three ways to adapt to attach a schema to such a collection:

  • Make each new field optional.
  • By default ( friends defaults to [] ).
  • Refresh the user interface to include new required elements (radio for "P = NP" or "P! = NP").

Each option is somewhat valid in its own right. Choose what seems most logical in the current context and which will give you the least headaches.

Do you really need a user-defined value for someField when it logs in? Then you must update the user interface to get this value.
Is the presence of someField important and can it be initialized with the default object (empty array, null , 0 ...)? Then the default value will match and it will be added when Collection2 clears the document.
None of the above? Not necessary.


As a somewhat personal note, I prefer this code:

 someUser.friends.forEach(sendGifts); 

For this:

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

friends is an optional field in the second code, so we are not sure if it is present or undefined. Calling forEach on undefined leads to a good big mistake, so we must first check for the presence of a field ... Thus, I would advise you to avoid optional fields a bit for consistency and simplicity .

-2
source

Source: https://habr.com/ru/post/987907/


All Articles