如何修复“需要路径,验证错误”,猫鼬,节点
Posted
技术标签:
【中文标题】如何修复“需要路径,验证错误”,猫鼬,节点【英文标题】:How to fix " Path is required ,ValidationError " ,Mongoose ,node 【发布时间】:2021-10-17 23:31:01 【问题描述】:我到处寻找这个问题。但我找不到修复它的正确方法。帮我看看是什么问题。
const router = require("express").Router();
let Reg = require("../models/Register");
http://localhost:8070/Register/add
router.route("/add").post((req,res)=>
const name = req.body.Name;
const email = req.body.Email;
const number = Number(req.body.Number);
const password = req.body.Password;
const NewAdd = new Reg(
name,
email,
number,
password
)
NewAdd.save().then(()=>
res.json("Registration Added")
).catch((err)=>
console.log(err);
res.json(err)
console.log("reg err");
)
)
这是一个模块文件夹 js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const RegSchema = new Schema(
Name:
type: String,
required: true
,
Email:
type: String,
required: true
,
Password:
type: String,
required: true
,
Number:
type: Number,
required: true
);
const Register = mongoose.model("Register",RegSchema);
module.exports = Register;
这是服务器 .js
const express = require("express");
const mongoose = require("mongoose");
const bodyParser =require("body-parser");
const cors = require("cors");
require("dotenv").config();
const app = express();
const PORT = process.env.PORT || 8070;
app.use(cors());
app.use(bodyParser.json());
const URL = process.env.MONGODB_URL;
mongoose.connect(URL,
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopologyL: true,
useFindAndModify: false
);
const connection = mongoose.connection;
connection.once("open", () =>
console.log("Mongodb connection success!");
)
const AddRouter = require("./routes/Registers.js");
app.use("/Register",AddRouter);
app.listen(PORT, () =>
console.log(`The port is : $PORT`);
)
显示错误
名称:ValidatorError:路径
Name
是必需的。 在验证 (C:\Users\Sliit\Desktop\Reg-Backend\Travel-management-system\node_modules\mongoose\lib\schematype.js:1270:13) 在 C:\Users\Sliit\Desktop\Reg-Backend\Travel-management-system\node_modules\mongoose\lib\schematype.js:1253:7 在 Array.forEach () 在 SchemaString.SchemaType.doValidate (C:\Users\Sliit\Desktop\Reg-Backend\Travel-management-system\node_modules\mongoose\lib\schematype.js:1198:14) 在 C:\Users\Sliit\Desktop\Reg-Backend\Travel-management-system\node_modules\mongoose\lib\document.js:2545:18 在 processTicksAndRejections (node:internal/process/task_queues:78:11) 属性:[对象], 种类:'必需', 路径:'名称', 值:未定义, 原因:未定义, [符号(猫鼬:validatorError)]:真 , _message: '注册验证失败'
我从邮递员那里测试 API 并收到此错误
“错误”: “数字”: "name": "ValidatorError", "message": "路径
Number
是必需的。", “特性”: "message": "路径Number
是必需的。", “类型”:“必需”, “路径”:“编号” , “种类”:“必需”, “路径”:“编号” , “密码”: "name": "ValidatorError", "message": "路径Password
是必需的。", “特性”: "message": "路径Password
是必需的。", “类型”:“必需”, “路径”:“密码” , “种类”:“必需”, “路径”:“密码” , “电子邮件”: "name": "ValidatorError", "message": "路径Name
是必需的。", “特性”: "message": "路径Name
是必需的。", “类型”:“必需”, “路径”:“名称” , “种类”:“必需”, “路径”:“名称” , "_message": "注册验证失败", “名称”:“验证错误”, "message": "注册验证失败:编号:路径Number
是必需的。密码:路径Password
是必需的。电子邮件:路径Name
是必需的。"
【问题讨论】:
【参考方案1】:在“NewAdd”和“RegSchema”中使用相同的大小写。
const NewAdd = new Reg(
name,
email,
number,
password
)
和
const RegSchema = new Schema(
name:
type: String,
required: true
,
email:
type: String,
required: true
,
password:
type: String,
required: true
,
number:
type: Number,
required: true
)
【讨论】:
以上是关于如何修复“需要路径,验证错误”,猫鼬,节点的主要内容,如果未能解决你的问题,请参考以下文章