Vadorequest Vadorequest - 3 months ago 18
Node.js Question

array of string in a schema mongoose

I'm trying to have a Role that contains an array of access.

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 replace
type: 'string'
by
type: String
it works. Why?
If I try to add an index on the array it doesn't works. (
index: true
)

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

Answer

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

Comments