Search code examples
node.jsmongodbmongoose-schema

Mongoose TypeError Invalid schema configuration


have I done something wrong in the schema?

const mongoose = require("mongoose");
const Schema = mongoose.Schema;


const userSchema = new Schema(
  {
    username: {
      name: String,
      required: true,
      unique: true,
      trim: true,
      minlength: 3
    },
  }, 
  {
    timestamps: true
  }
);

const User = mongoose.model("User", userSchema);

module.exports = User;

The error I get

throw new TypeError(`Invalid schema configuration: \`${name}\` is 
not ` + ^ TypeError: Invalid schema configuration: `True` is not a valid type

Solution

  • I think name is not valid attribute in mongoose schemas. Try to replace name by type and correct the minlength typo to minLength:

    const userSchema = new Schema(
      {
        username: {
          type: String,
          required: true,
          unique: true,
          trim: true,
          minLength: 3
        },
      }, 
      {
        timestamps: true
      }
    );