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

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

Mongoose Post request still works when a required field is missing

问题

以下是您要翻译的内容:

我有一个用户模型看起来像这样

const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const userSchema = new Schema({

    fullName: {
        type: String,
        require: true
    },
    email: {
        type: String,
        require: true
    }
})

module.exports = mongoose.model('User', userSchema)

接下来我的控制器函数如下所示

exports.register = (req, res) => {
    
    const email = req.body.email
    const name = req.body.fullName

    bcrypt
        .hash(password, 12)
        .then(hashedPassword => {
            const user = new User({
                email: email,
                password: hashedPassword,
                name: name
            })
            return user.save()
        })
        .then(createdUser => {
            res.status(201).json({ message: "user created", userData: createdUser })
        })
        .catch(err => {
            if(!err.statusCode) {
                err.statusCode = 500
            }
            next(err)
        })
}

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

payload:

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

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


<details>
<summary>英文:</summary>

I have a user model which looks like this:

    const mongoose = require(&#39;mongoose&#39;)
    const Schema = mongoose.Schema;
    const userSchema = new Schema({
    
        fullName: {
            type: String,
            require: true
        },
        email: {
            type: String,
            require: true
        }
    })
    
    module.exports = mongoose.model(&#39;User&#39;, userSchema)

Moving forward, my controller function looks like this:

    exports.register = (req, res) =&gt; {
        
        const email = req.body.email
        const name = req.body.fullName
    
        bcrypt
            .hash(password, 12)
            .then(hashedPassword =&gt; {
                const user = new User({
                    email: email,
                    password: hashedPassword,
                    name: name
                })
                return user.save()
            })
            .then(createdUser =&gt; {
                res.status(201).json({ message: &quot;user creared&quot;, userData: createdUser })
            })
            .catch(err =&gt; {
                if(!err.statusCode) {
                    err.statusCode = 500
                }
                next(err)
            })
    }

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.

payload:

    {
      &quot;email&quot;: &quot;john@example.com&quot;,
      &quot;number&quot;: &quot;119299928&quot;,
      &quot;password&quot;: &quot;encrypted password&quot;,
      &quot;status&quot;: &quot;active&quot;,
      &quot;role&quot;: &quot;administrator&quot;
    }

I would expect an exception to be thrown if a field is required, but the user actually gets created.
Am I missing anything?

</details>


# 答案1
**得分**: 1

尝试将 `require: true` 更改为模式声明中的 `required: true`。

<details>
<summary>英文:</summary>

Try changing `require: true`, to `required: true`, at the schema delcaration.


</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:

确定