【问题标题】:How to fix "User validation failed: password: Path `password` is required., email: Path `email` is required., name: Path `name` is required."如何解决“用户验证失败:密码:路径‘密码’是必需的。电子邮件:路径‘电子邮件’是必需的。名称:路径‘名称’是必需的。”
【发布时间】:2021-01-17 23:52:22
【问题描述】:

我是一个开始学习 node.js 的初学者。在中途,我遇到了这样的通知问题

{
"errors": {
    "password": {
        "name": "ValidatorError",
        "message": "Path `password` is required.",
        "properties": {
            "message": "Path `password` is required.",
            "type": "required",
            "path": "password"
        },
        "kind": "required",
        "path": "password"
    },
    "email": {
        "name": "ValidatorError",
        "message": "Path `email` is required.",
        "properties": {
            "message": "Path `email` is required.",
            "type": "required",
            "path": "email"
        },
        "kind": "required",
        "path": "email"
    },
    "name": {
        "name": "ValidatorError",
        "message": "Path `name` is required.",
        "properties": {
            "message": "Path `name` is required.",
            "type": "required",
            "path": "name"
        },
        "kind": "required",
        "path": "name"
    }
},
"_message": "User validation failed",
"message": "User validation failed: password: Path `password` is required., email: Path `email` is required., name: Path `name` is required."}

这是我的用户模型

const mongoose = require('mongoose')
const validator = require('validator')

const User = mongoose.model('User', {
    name: {
        type: String,
        required: true,
        trim: true
    }, 
    email: {
        type: String, 
        required: true,
        trim: true,
        lowercase: true,
        validate(value) {
            if (!validator.isEmail(value)) {
                throw new Error('Email is invalid')
            }
        }
    },
    password:{
        type: String,
        required: true, 
        minlength: 7,
        trim: true,
         validate(value){
            if (value.toLowerCase().includes('password')) {
                throw new Error('Password cannot contain "password"')
            }
        }
    },
    age: {
        type: Number, 
        default: 0,
        validate(value) {
            if (value < 0 ){
                throw new Error('Age must be a positive number')
            }
        }
    }
})

module.exports = User

使用此用户路线

const express = require('express')
require('./db/mongoose')
const User = require('./models/user')
const Task = require('./models/task')

const app = express()
const port = process.env.PORT || 3000

app.use(express.json())

app.post('/users', async (req, res) => {
    const user = new User(req.body)

    try{
        await user.save()
        res.status(201).send(user)
    } catch (e) {
        res.status(400).send(e)
    }
})



app.listen(port, () => {
    console.log('Server is up on port' +port)
})

有人知道为什么会这样吗?

希望我能在这个论坛上得到答案,继续我的学习。预先感谢您的帮助。真的很感激。

【问题讨论】:

    标签: node.js nodes validationerror


    【解决方案1】:

    由于您已根据要求定义了所有这些字段:在您的架构中为真,这就是它发生的原因,您必须为这些字段提供值,即在测试您的 api 时,请在正文中填充这些字段。

    【讨论】:

      【解决方案2】:

      试试这个, 就我而言,我使用了这个

      app.use(express.json())
      

      下面

      app.use("/users",usersController);
      

      所以,这会导致问题,因为我的数据没有正确解析。

        const express = require("express");
          const app = express();
          const connect = require("./config/db");
          const usersController = require("./controller/user.controller");
          
          app.use(express.json());
          
          app.use("/users",usersController);
          
          
          
          const start = async () => {
            await connect();
            app.listen(2000, async function () {
              console.log("listening on port 2000");
            });
          };
          module.exports = start;
      

      【讨论】:

        【解决方案3】:

        在发送之前,在您的“邮递员”中,更改为“body”,然后更改为“raw”模块,在“Text”所在的末尾,更改为“JSON”,这样您的代码就可以工作了。

        【讨论】:

        • 我不确定我是否在这里看到了相关性,抱歉。 OP没有提到任何关于邮递员的事情。他们后端的验证库似乎有问题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-07
        • 2020-02-28
        • 1970-01-01
        • 2020-11-26
        • 2016-10-30
        • 1970-01-01
        • 2022-01-21
        相关资源
        最近更新 更多