无法将我的猫鼬模型添加到数据库中

法里德·加德里(Farid Ghaderi)

我有一个猫鼬模型,我想将我的请求正文数据传递给它,然后使用create函数将其添加到我的数据库中,但是它不起作用,并且不会给我任何错误,它只是在请求中存入了我的模特

const usersSchema = new mongoose.Schema({
  firstname: {
    type: String,
    required: [true, "first name is required"],
    trim: true,
    minlength: {
      value: 2,
      message: "your first name could not be less than 2 characters!",
    },
    maxlength: {
      values: 25,
      message: "your first name could not be more than 25 characters!",
    },
  },
  lastname: {
    type: String,
    required: [true, "last name is required"],
    trim: true,
    minlength: {
      values: 2,
      message: "your last name could not be less than 2 characters!",
    },
    maxlength: {
      values: 35,
      message: "your last name could not be more than 35 characters!",
    },
  },
  username: {
    type: String,
    unique: [true, "username has already been used!"],
    required: [true, "username is required!"],
    lowercase: true,
    trim: true,
    minlength: {
      values: 6,
      message: "your username must be at least 6 characters",
    },
    maxlength: {
      values: 35,
      message: "your user name could not be bigger than 35 characters",
    },
  },
  userType: {
    type: String,
    required: [true, "usertype is required"],
    trim: true,
    lowercase: true,
    enum: {
      values: ["user", "admin"],
      message: "each user can only be admin or user",
    },
  },
  password: {
    type: String,
    required: [true, "please enter a password for your account"],
    select: false,
    minlength: [8, "your password could not be less than 8 characters"],
    maxlength: [64, "your password could not be more than 64 characters"],
  },
  confirmPassword: {
    type: String,
    required: [true, "please confirm your password"],
    validate: {
      validator: function (val) {
        return this.password === val;
      },
      message: "your passwords are not same",
    },
  },
  email: {
    type: String,
    lowercase: true,
    trim: true,
    unique: [true, "this email has already been used!"],
  },
  phoneNumber: {
    type: String,
    unique: [true, "this phone number has already been used!"],
    minlength: [11, "phone numbers has the length of 11 characters!"],
    maxlength: [11, "phone numbers has the length of 11 characters!"],
  },
  gender: {
    type: String,
    required: [true, "gender field could not be empty!"],
    enum: {
      values: ["male,female"],
      message: "you must be either male or female!",
    },
  },
  birthDate: {
    type: Date,
  },
  confirmedEmail: {
    type: Boolean,
    required: [true, "email confirmation must be specified as true or false"],
  },
  confirmedPhone: {
    type: Boolean,
    required: [
      true,
      "phone number confirmation must be specified as true or false",
    ],
  },
  userTickets: [
    {
      matchId: {
        type: String,
        required: [true, "matchId could not be empty!"],
      },
      ticketId: {
        type: String,
        required: [true, "matchId could not be empty!"],
      },
    },
  ],
});
const userModel = mongoose.model("users", usersSchema);

module.exports = userModel;
 

这是我的控制器

exports.insertUser = async (req, res) => {
  await userModel.create(req.body);
  res.status(201).json({
    status: "success",
    message: "documents added to DB successfully",
  });
};

我更改了模型,但可以使用,但不适用于该模型

穆罕默德·亚瑟·艾哈迈迪

像这样更改架构:

{
  firstname: {
    type: String,
    required: [true, "first name is required"],
    trim: true,
    minlength: [
       2,
      "your first name could not be less than 2 characters!",
    ],
    maxlength: [
      25,
      "your first name could not be more than 25 characters!"
    ]
  },
  lastname: {
    type: String,
    required: [true, "last name is required"],
    trim: true,
    minlength: [
      2,
     "your last name could not be less than 2 characters!",
   ],
   maxlength: [
     25,
     "your last name could not be more than 25 characters!"
   ]
  },
  username: {
    type: String,
    unique: [true, "username has already been used!"],
    required: [true, "username is required!"],
    lowercase: true,
    trim: true,
    minlength: [
      2,
     "your user name could not be less than 2 characters!",
   ],
   maxlength: [
     35,
     "your user name could not be more than 25 characters!"
   ]
  },
  userType: {
    type: String,
    required: [true, "usertype is required"],
    trim: true,
    lowercase: true,
    enum: {
      values: ["user", "admin"],
      message: "each user can only be admin or user",
    },
  },
  password: {
    type: String,
    required: [true, "please enter a password for your account"],
    select: false,
    minlength: [8, "your password could not be less than 8 characters"],
    maxlength: [64, "your password could not be more than 64 characters"],
  },
  confirmPassword: {
    type: String,
    required: [true, "please confirm your password"],
    validate: {
      validator: function (val) {
        return this.password === val;
      },
      message: "your passwords are not same",
    },
  },
  email: {
    type: String,
    lowercase: true,
    trim: true,
    unique: [true, "this email has already been used!"],
  },
  phoneNumber: {
    type: String,
    unique: [true, "this phone number has already been used!"],
    minlength: [11, "phone numbers has the length of 11 characters!"],
    maxlength: [11, "phone numbers has the length of 11 characters!"],
  },
  gender: {
    type: String,
    required: [true, "gender field could not be empty!"],
    enum: {
      values: ["male", "female"],
      message: "you must be either male or female!",
    },
  },
  birthDate: {
    type: Date,
  },
  confirmedEmail: {
    type: Boolean,
    required: [true, "email confirmation must be specified as true or false"],
  },
  confirmedPhone: {
    type: Boolean,
    required: [
      true,
      "phone number confirmation must be specified as true or false",
    ],
  },
  userTickets: [
    {
      matchId: {
        type: String,
        required: [true, "matchId could not be empty!"],
      },
      ticketId: {
        type: String,
        required: [true, "matchId could not be empty!"],
      },
    },
  ],
}

因为使用ofminlengthmaxlength使用以下格式:

    minlength: [
       2,
      "your first name could not be less than 2 characters!",
    ]

values: ["male","female"]性别,而不是values: ["male,female"]因为你缺少"

在身体要求的样本中

 {
    "lastname": "name",
    "firstname": "family",
    "username": "asf3",
    "userType": "admin",
    "password": "123456789",
    "confirmPassword": "123456789",
    "email": "[email protected]",
    "phoneNumber": "12123456789",
    "gender": "female",
    "birthDate": "2002-06-05",
    "confirmedEmail": true,
    "confirmedPhone": true,
    "userTickets": []
  }

req.body样品中使用firstName,并lastName用大写字母N,但在模式中firstnamelastname

使用try / catch这样:

exports.insertUser = async (req, res) => {
  try {
    let newUser = new userModel(req.body)
    await newUser.save();
    res.status(201).json({
      status: "success",
      message: "documents added to DB successfully",
    });
  } catch (error) {
    console.log(error);
    res.status(500).json({
      status: "faild",
      message: "something went wrong",
    });
  }
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

猫鼬将多个项目添加到数据库

来自分类Dev

无法将模型添加到mongo数据库。

来自分类Dev

在猫鼬模型中添加到“人口”对象

来自分类Dev

无法将元素添加到模型类的 ICollection 或更新数据库中的外键

来自分类Dev

猫鼬在数据库中的模型名称后面添加“ s”?

来自分类Dev

猫鼬在数据库中的模型名称后面添加“ s”?

来自分类Dev

无法将BOOLEAN列添加到我的Derby数据库中的表中

来自分类Dev

我似乎无法将记录添加到Visual Studio中的数据库中

来自分类Dev

无法添加到数据库

来自分类Dev

无法使用猫鼬从数据库中读取

来自分类Dev

无法将“用户”数据模型添加到数据库

来自分类Dev

我无法将发布的数据添加到从在 PHP 中执行的 javascript 发送的数据库中

来自分类Dev

在猫鼬中复制数据库

来自分类Dev

我无法使用AngularJs将数据从文本框添加到数据库

来自分类Dev

我如何在猫鼬对象上添加临时属性,仅用于响应,而不存储在数据库中

来自分类Dev

将字段添加到Rails模型而不将其保存在Rails的数据库中

来自分类Dev

将列添加到数据库而不用实体框架在模型中定义它

来自分类Dev

用猫鼬创建数据库模型

来自分类Dev

将元数据添加到猫鼬字段

来自分类Dev

猫鼬:将数据添加到返回的结果集中

来自分类Dev

将元数据添加到猫鼬字段

来自分类Dev

涉及SQLite 3的错误使我无法正确添加到数据库

来自分类Dev

无法使用PHP的jQuery Ajax将数据添加到数据库中

来自分类Dev

将下拉数据添加到我的数据库中

来自分类Dev

如何将新的数据行添加到我的 Derby 数据库中?

来自分类Dev

猫鼬-这个额外的_id属性被添加到我的友谊属性中是什么?

来自分类Dev

无法将项目添加到数据库

来自分类Dev

无法将日期添加到Access数据库

来自分类Dev

Node.js,MongDB(猫鼬)-添加到检索到的数据中。

Related 相关文章

  1. 1

    猫鼬将多个项目添加到数据库

  2. 2

    无法将模型添加到mongo数据库。

  3. 3

    在猫鼬模型中添加到“人口”对象

  4. 4

    无法将元素添加到模型类的 ICollection 或更新数据库中的外键

  5. 5

    猫鼬在数据库中的模型名称后面添加“ s”?

  6. 6

    猫鼬在数据库中的模型名称后面添加“ s”?

  7. 7

    无法将BOOLEAN列添加到我的Derby数据库中的表中

  8. 8

    我似乎无法将记录添加到Visual Studio中的数据库中

  9. 9

    无法添加到数据库

  10. 10

    无法使用猫鼬从数据库中读取

  11. 11

    无法将“用户”数据模型添加到数据库

  12. 12

    我无法将发布的数据添加到从在 PHP 中执行的 javascript 发送的数据库中

  13. 13

    在猫鼬中复制数据库

  14. 14

    我无法使用AngularJs将数据从文本框添加到数据库

  15. 15

    我如何在猫鼬对象上添加临时属性,仅用于响应,而不存储在数据库中

  16. 16

    将字段添加到Rails模型而不将其保存在Rails的数据库中

  17. 17

    将列添加到数据库而不用实体框架在模型中定义它

  18. 18

    用猫鼬创建数据库模型

  19. 19

    将元数据添加到猫鼬字段

  20. 20

    猫鼬:将数据添加到返回的结果集中

  21. 21

    将元数据添加到猫鼬字段

  22. 22

    涉及SQLite 3的错误使我无法正确添加到数据库

  23. 23

    无法使用PHP的jQuery Ajax将数据添加到数据库中

  24. 24

    将下拉数据添加到我的数据库中

  25. 25

    如何将新的数据行添加到我的 Derby 数据库中?

  26. 26

    猫鼬-这个额外的_id属性被添加到我的友谊属性中是什么?

  27. 27

    无法将项目添加到数据库

  28. 28

    无法将日期添加到Access数据库

  29. 29

    Node.js,MongDB(猫鼬)-添加到检索到的数据中。

热门标签

归档