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')) {
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 .
source share