Meteor简单模式-当修饰符选项为true时,验证对象必须至少有一个运算符

Meteor Simple Schema - When the modifier option is true, validation object must have at least one operator

本文关键字:对象 验证 运算符 有一个 true 模式 简单 选项 Meteor      更新时间:2023-09-26

当我尝试创建用户时,我一直得到错误:Exception while invoking method 'createUser' Error: When the modifier option is true, validation object must have at least one operator。我使用的是流星简单模式,但没有一个修复这个错误的方法对我有效。我尝试过使用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: []
      }
    }
  }
});

有什么想法可以让它发挥作用吗?

您忘记在开头添加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: []
  }
});

这本书更短,更容易阅读。