mongoose 错误:需要找到验证程序路径错误

qpgpyjmq  于 2022-11-13  发布在  Go
关注(0)|答案(1)|浏览(176)

Mongooose字符串验证是路径所需的错误编号是工作,但其他不工作,我坚持过去一周仍然是错误是不去是我的代码,然后字符串删除一个我工作的所有步骤,然后错误不删除
Mongooose字符串验证是路径所需的错误编号是工作,但其他不工作,我坚持过去一周仍然是错误是不去是我的代码,然后字符串删除一个我工作的所有步骤,然后错误不删除

const validator = require("validator");

const employeeSchema = new mongoose.Schema({
    firstname: {
        type: String,
        required: true,
    },

    lastname: {
        type: String,
        required: true,
    },
    email: {
        type: String,
        required: true,
    },
    gender: {
        type: String,
        required: true,
    },

    phone: {
        type: Number,
        required: true,
    },
    age: {
        type: Number,
        required: true,
    },
    password: { type: String, required: true },
    confirmpassword: { type: String, required: true },
});

// now we need to create a collections

const Register = new mongoose.model("Register", employeeSchema);

module.exports = Register;



const express = require("express");
const app = express();
const path = require("path");
const hbs = require("hbs");
require("./db/conn");
const Register = require("./models/registers");
const port = process.env.PORT || 3000;
const static_path = path.join(__dirname, "../public");
const template_path = path.join(__dirname, "../templates/views");
const partials_path = path.join(__dirname, "../templates/partials");
// const reg_path = path.join(__dirname, "../src/models/registers");
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// app.use(express.urlencoded({ extended: false }));

app.use(express.static(static_path));
app.set("view engine", "hbs");
app.set("views", template_path);
hbs.registerPartials(partials_path);

app.get("/", (req, res) => {
    res.render("index");
});
app.get("/register", (req, res) => {
    res.render("register");
});

// create a new user in our database
app.post("/register", async(req, res) => {
    try {
        // console.log(req.body.firstname);
        // res.send(req.body.firstname);
        const password = req.body.password;
        const cpassword = req.body.confirmpassword;
        // const afirstname = JSON.stringify(firstname);
        // const alastname = JSON.stringify(lastname);
        // const aemail = JSON.parse(email);
        // const agender = JSON.parse(gender);

        if (password === cpassword) {
            const registerEmployee = new Register({
                firstname: req.body.firstname,
                firstname: req.body.lastname,
                email: req.body.email,
                gender: req.body.gender,
                phone: req.body.phone,
                age: req.body.age,
                password: password,
                confirmpassword: cpassword,
            });
            console.log(registerEmployee);
            const registered = await registerEmployee.save();
            res.status(201).json(registered);
        } else {
            res.send("password are not matching");
        }
    } catch (e) {
        res.status(400).send(e);
    }
});

app.listen(port, () => {
    console.log(`server is running at port no ${port}`);
});


{
"errors": {
"lastname": {
"name": "ValidatorError",
"message": "Path `lastname` is required.",
"properties": {
"message": "Path `lastname` is required.",
"type": "required",
"path": "lastname"
},
"kind": "required",
"path": "lastname"
},
"firstname": {
"name": "ValidatorError",
"message": "Path `firstname` is required.",
"properties": {
"message": "Path `firstname` is required.",
"type": "required",
"path": "firstname"
},
"kind": "required",
"path": "firstname"
},
"email": {
"name": "ValidatorError",
"message": "Path `email` is required.",
"properties": {
"message": "Path `email` is required.",
"type": "required",
"path": "email"
},
"kind": "required",
"path": "email"
},
"gender": {
"name": "ValidatorError",
"message": "Path `gender` is required.",
"properties": {
"message": "Path `gender` is required.",
"type": "required",
"path": "gender"
},
"kind": "required",
"path": "gender"
},
"password": {
"name": "ValidatorError",
"message": "Path `password` is required.",
"properties": {
"message": "Path `password` is required.",
"type": "required",
"path": "password"
},
"kind": "required",
"path": "password"
},
"confirmpassword": {
"name": "ValidatorError",
"message": "Path `confirmpassword` is required.",
"properties": {
"message": "Path `confirmpassword` is required.",
"type": "required",
"path": "confirmpassword"
},
"kind": "required",
"path": "confirmpassword"
}
},
"_message": "Register validation failed",
"name": "ValidationError",
"message": "Register validation failed: lastname: Path `lastname` is required., firstname: Path `firstname` is required., email: Path `email` is required., gender: Path `gender` is required., password: Path `password` is required., confirmpassword: Path `confirmpassword` is required."
}```
neekobn8

neekobn81#

在此文件中使用此代码

const mongoose = require("mongoose");
const validator = require("validator");

const employeeSchema = new mongoose.Schema({
    firstname:{
        type:String,
        required:true
    },
    lastname:{
        type:String,
        required:true
    },
    number:{
        type:Number,
        required:true,
        unique:true
    },
    email:{
        type:String,
        required:true,
        unique:true
    },
    password:{
        type:String,
        required:true
    },
    cpassword :{
        type:String,
        required:true
    },
});

//now we need to create a collections
const Register = new mongoose.model("Register",employeeSchema);
module.exports = Register;

相关问题