Meteor Simple Schema - When the modifier option is

2019-08-10 11:34发布

问题:

I keep getting the error: Exception while invoking method 'createUser' Error: When the modifier option is true, validation object must have at least one operator when I try to create a user. I am using meteor-simple-schema, but none of the fixes with this error have worked for me. I have tried using blackbox and optional to see where the issue is but I keep getting the same error.

var Schemas = {};    
Schemas.UserGamesPart = {
  public: {
    type: [String],
    defaultValue: []
  },
  private: {
    type: [String],
    defaultValue: []
  }
};
Schemas.UserGames = {
  game1: {
    type: Schemas.UserGamesPart
  }
};
Schemas.UserProfile = new SimpleSchema({
  games: {
    type: Schemas.UserGames
  }
});
Schemas.UpgradeDetails = new SimpleSchema({
  subscribed_on: {
    type: Date,
    optional: true
  },
  stripe_charge_id: {
    type: String,
    optional: true
  },
  school_license: {
    type: Boolean,
    defaultValue: false,
    optional: true
  }
});
Schemas.UserProperties = new SimpleSchema({
  paid: {
    type: Boolean,
    defaultValue: false
  },
  upgrade_details: {
    type: Schemas.UpgradeDetails,
    optional: true
  }
});

Schemas.User = new SimpleSchema({
  _id: {
    type: String
  },
  username: {
    type: String,
    optional: true
  },
  emails: {
    type: [Object]
  },
  "emails.$.address": {
    type: String,
    regEx: SimpleSchema.RegEx.Email,
    optional: true
  },
  "emails.$.verified": {
    type: Boolean,
    optional: true
  },
  createdAt: {
    type: Date
  },
  profile: {
    type: Schemas.UserProfile,
    blackbox: true,
    optional: true
  },
  properties: {
    type: Schemas.UserProperties,
    blackbox: true,
    optional: true
  }
});

Meteor.users.attachSchema(Schemas.User);

My accounts.creaate user is as follows:

Accounts.createUser({
  email: $('#email').val(),
  password: $('#password').val(),
  profile: {
    games: {
      game1: {
        public: [],
        private: []
      }
    }
  }
});

Any ideas on how I can get this to work?

回答1:

You forgot to add new SimpleSchema there in the beginning:

Schemas.UserGamesPart = new SimpleSchema({
  public: {
    type: [String],
    defaultValue: []
  },
  private: {
    type: [String],
    defaultValue: []
  }
});
Schemas.UserGames = new SimpleSchema({
  game1: {
    type: Schemas.UserGamesPart
  }
});

Also I think your usage of the nested schemas is a little off. Only nest schemas when you need to reuse one. Creating a separate schema for UserGamesPart looks horrible. Try this instead:

Schemas.UserGames = new SimpleSchema({
  game1: {
    type: Object
  }
  'game1.public': {
    type: [String],
    defaultValue: []
  },
  'game1.private': {
    type: [String],
    defaultValue: []
  }
});

This is shorter and easier to read.