【问题标题】:NodeJS - MongooseJS schema error that i cant figure outNodeJS - 我无法弄清楚的 Mongoose JS 架构错误
【发布时间】:2015-10-15 00:41:16
【问题描述】:

也许第二双眼睛可以看到我的架构出了什么问题

var UserSchema = new Schema({
        name: 
                {
                        first : {type: String}
                    ,   last : {type : String}
                }
    ,   password: {type: String}
    ,   username: {type: String}
    , role: RoleSchema
  , created_at  : {type : Date, default : Date.now}
  , modified_at  : {type : Date, default : Date.now}
})

var RoleSchema = {
        type: [String]
    ,   study_type: [String]
}

mongoose.model('User', UserSchema)

错误:

TypeError: Invalid value for schema path `role`

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

    嵌入式架构(角色)需要位于 UserSchema 之上

    【讨论】:

      【解决方案2】:

      除了必须在 UserSchema 之前导入 Roles 架构。

      在较新版本的 mongoose 中,还需要使用以下语法来超越 'TypeError: Invalid value for schema Array path

      var SomeSchema = new mongoose.Schema();
      
        SomeSchema.add({
          key1: {
            type: String,
            required: true
          },
          key2: {
            type: String,
            required: true
          },
          key3: {
            type: String,
            required: true
          }
        });
      
        SomeSchema.get(function(val){
          // Remove the _id from the Violations
          delete val._id;
          return val;
        });
      

      还有父母:

      var ParentSchema = new mongoose.Schema({
          parentKey: String,
          someArray: [SomeSchema]
      })
      
      module.exports = mongoose.model('Parent', ParentSchema)
      

      从 mongoose 3.x 切换到 4.x 时发生这种情况

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-03
        • 2022-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多