Home > Software engineering >  Mongoose TypeError Invalid schema configuration
Mongoose TypeError Invalid schema configuration

Time:03-07

been sitting on this for hours.. have I done something wrong in the schema? Someone kindly provide a solution

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

CodePudding user response:

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
  }
);

CodePudding user response:

You should change the schema to below

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

instead of 'name' just use 'type'

For further reference follow this link https://mongoosejs.com/docs/guide.html

  • Related