Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS - MongooseJS schema error that i cant figure out

Maybe a second set of eyes can see 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`
like image 468
Vartan Arabyan Avatar asked Jul 05 '12 23:07

Vartan Arabyan


2 Answers

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

like image 115
Menztrual Avatar answered Oct 20 '22 08:10

Menztrual


In addition to the Roles schema having to be imported before the UserSchema.

In the newer versions of mongoose the following sort of syntax was also needed for to get beyond the '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;
  });

And the parent:

var ParentSchema = new mongoose.Schema({
    parentKey: String,
    someArray: [SomeSchema]
})

module.exports = mongoose.model('Parent', ParentSchema)

This happened when switching from mongoose 3.x to 4.x

like image 20
jmunsch Avatar answered Oct 20 '22 08:10

jmunsch