Mongoose TypeError 模式配置无效

Mongoose TypeError Invalid schema configuration

我是不是在架构中做错了什么?

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;

我得到的错误

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

我认为 name 在猫鼬模式中不是有效属性。尝试将 name 替换为 type 并将 minlength 拼写错误更正为 minLength:

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

您应该将架构更改为以下

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

而不是 'name' 只需使用 'type'

如需进一步参考,请遵循此 link https://mongoosejs.com/docs/guide.html