Meteor Simple Schema - 当modifier选项为true时,验证对象必须至少有一个运算符

时间:2015-06-27 20:26:19

标签: javascript mongodb meteor schema

当我尝试创建用户时,我不断收到错误:Exception while invoking method 'createUser' Error: When the modifier option is true, validation object must have at least one operator。我正在使用meteor-simple-schema,但没有任何修复此错误对我有用。我尝试过使用blackbox和optional来查看问题所在,但我不断收到同样的错误。

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);

我的accounts.creaate用户如下:

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

关于如何让它发挥作用的任何想法?

1 个答案:

答案 0 :(得分:2)

您忘了在开头添加new SimpleSchema

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

另外我认为你对嵌套模式的使用有点偏。当您需要重用一个时,嵌套模式。为UserGamesPart创建单独的架构看起来很糟糕。试试这个:

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

这更短,更容易阅读。