Mongoose Post请求在缺少必需字段时仍然有效。

huangapple go评论86阅读模式
英文:

Mongoose Post request still works when a required field is missing

问题

以下是您要翻译的内容:

  1. 我有一个用户模型看起来像这样
  2. const mongoose = require('mongoose')
  3. const Schema = mongoose.Schema;
  4. const userSchema = new Schema({
  5. fullName: {
  6. type: String,
  7. require: true
  8. },
  9. email: {
  10. type: String,
  11. require: true
  12. }
  13. })
  14. module.exports = mongoose.model('User', userSchema)
  15. 接下来我的控制器函数如下所示
  16. exports.register = (req, res) => {
  17. const email = req.body.email
  18. const name = req.body.fullName
  19. bcrypt
  20. .hash(password, 12)
  21. .then(hashedPassword => {
  22. const user = new User({
  23. email: email,
  24. password: hashedPassword,
  25. name: name
  26. })
  27. return user.save()
  28. })
  29. .then(createdUser => {
  30. res.status(201).json({ message: "user created", userData: createdUser })
  31. })
  32. .catch(err => {
  33. if(!err.statusCode) {
  34. err.statusCode = 500
  35. }
  36. next(err)
  37. })
  38. }

然而,当我在Postman中创建用户时,即使fullName字段是必需的,如果payload中不存在fullName字段,也不会引发错误。

payload:

{
"email": "john@example.com",
"number": "119299928",
"password": "加密的密码",
"status": "active",
"role": "administrator"
}

我希望如果字段是必需的,如果用户实际上被创建,会引发异常。我有遗漏什么吗?

  1. <details>
  2. <summary>英文:</summary>
  3. I have a user model which looks like this:
  4. const mongoose = require(&#39;mongoose&#39;)
  5. const Schema = mongoose.Schema;
  6. const userSchema = new Schema({
  7. fullName: {
  8. type: String,
  9. require: true
  10. },
  11. email: {
  12. type: String,
  13. require: true
  14. }
  15. })
  16. module.exports = mongoose.model(&#39;User&#39;, userSchema)
  17. Moving forward, my controller function looks like this:
  18. exports.register = (req, res) =&gt; {
  19. const email = req.body.email
  20. const name = req.body.fullName
  21. bcrypt
  22. .hash(password, 12)
  23. .then(hashedPassword =&gt; {
  24. const user = new User({
  25. email: email,
  26. password: hashedPassword,
  27. name: name
  28. })
  29. return user.save()
  30. })
  31. .then(createdUser =&gt; {
  32. res.status(201).json({ message: &quot;user creared&quot;, userData: createdUser })
  33. })
  34. .catch(err =&gt; {
  35. if(!err.statusCode) {
  36. err.statusCode = 500
  37. }
  38. next(err)
  39. })
  40. }
  41. However, when I go to Postman and create the user, even though the fullName field is required, no error is thrown if the fullName field does not exist in the payload.
  42. payload:
  43. {
  44. &quot;email&quot;: &quot;john@example.com&quot;,
  45. &quot;number&quot;: &quot;119299928&quot;,
  46. &quot;password&quot;: &quot;encrypted password&quot;,
  47. &quot;status&quot;: &quot;active&quot;,
  48. &quot;role&quot;: &quot;administrator&quot;
  49. }
  50. I would expect an exception to be thrown if a field is required, but the user actually gets created.
  51. Am I missing anything?
  52. </details>
  53. # 答案1
  54. **得分**: 1
  55. 尝试将 `require: true` 更改为模式声明中的 `required: true`。
  56. <details>
  57. <summary>英文:</summary>
  58. Try changing `require: true`, to `required: true`, at the schema delcaration.
  59. </details>

huangapple
  • 本文由 发表于 2023年6月26日 21:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定