Express always reports an error when using express-validator verification. Ask for advice.

server.js

const express = require("express");
const expressValidator = require("express-validator");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const app = express();

const port = 5846;

/**
 * 
 */
const userApi = require("./router/api/user");

const db = require("./config/key").mongoURI;
mongoose.connect(db,{ useNewUrlParser: true }).then( () => console.log("")).catch((err) => console.log(err));

app.use(bodyParser.urlencoded({extended:false}))  
app.use(bodyParser.json());
app.use(expressValidator());

/**
 * 
 */
app.use("/api/user",userApi);



/**
 *  app
 */
app.listen(port,() => {
    //
    console.log("")
});

router / api / user.js is the interface file in server.js

 const express = require("express");
 const {checkBody} = require("express-validator");
 const bcrypt = require("bcryptjs");
 const router = express.Router();
 const UserModel = require("../../models/User");

/**
  * 
  * POST api/user/register
  */
router.post("/register",(req,res) => {
    req.checkBody("userName", "Enter a user name.").notEmpty().trim();
    req.checkBody("userEmail","Enter a valid email address.").notEmpty();
    req.checkBody("userPassword","Enter a user password.").notEmpty();
    
    UserModel.findOne({userName:req.body.userName})
        .then((user) => {
            if(user){
                return res.status(400).json({userName:""})
            }else{
                const newUser = new UserModel({
                    userName: req.body.userName,
                    userEmail:req.body.userEmail,
                    userPassword: req.body.userPassword,
                    userDate:req.body.userDate
                });

                //
                bcrypt.genSalt(10, function(err, salt) {
                    bcrypt.hash(newUser.userPassword, salt, (err, hash) => {
                        if(err){
                            console.log(err);
                            res.send({
                                message :"Post request error!"
                            });
                        }
                        newUser.userPassword = hash;

                        //
                        newUser.save()
                                 .then(user => res.json(user))
                                 .catch(err => console.log(err));
                    });
                });
            }
        });
}

use postman to test data and return error message:

clipboard.png

:

clipboard.png

I don"t know what"s wrong with this reason. Ask for advice.

does it have to be verified before the submission can be successful?

Mar.02,2022

I see that the data you validate is passed from head, but it is obtained from body in the code. You can try to modify it, or you can use trycatch


you are verifying the parameters in body. The parameters sent by postman are in header, and the verifier cannot find the parameter path to be verified in your body

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea74b0-1e40.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea74b0-1e40.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?