Array of string in mongoose schema

I am trying to have a role containing an access array.

access: [{
    type: 'string',
    match: /^[a-zA-Z]+$/,
    required: true,
    notEmpty: true,
    check: {
        minLength: 2
    }
}]

I get:

node_modules\mongoose\lib\schema\array.js:58
    this.caster = new caster(null, castOptions);
                  ^
TypeError: string is not a function

If I replaced type: 'string'with type: String, it will work. What for? If I try to add an index to an array, it will not work. ( index: true)

Do I need to do collection.index({'access': 1})?

+3
source share
1 answer

type: String, is what you want in line 2. Mongoose expects the type to point to a function that can be used to force values ​​to the correct type.

+4
source

All Articles