NodeJS - 我无法弄清楚的MongooseJS架构错误

时间:2012-07-05 23:17:41

标签: node.js mongoose

也许第二组眼睛可以看到我的架构有什么问题

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`

2 个答案:

答案 0 :(得分:18)

嵌入式架构(角色)需要高于UserSchema

答案 1 :(得分:1)

除了必须在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

时发生这种情况