Search code examples
node.jsmongoose

Node.js - MongooseJS schema error that I can’t figure out


What is wrong with my schema?

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)

The error:

TypeError: Invalid value for schema path `role`

Solution

  • The embedded Schema (Roles) needs to be above the UserSchema