我到处找这个问题,但是我找不到正确的方法来解决它。请帮助我解决这个问题。
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}`);
})
错误显示
名称:验证器错误:路径Name
是必需的。验证时(C:\用户\滑动\桌面\注册表后端\差旅管理系统\节点模块\mongoose\lib\架构类型. js:1270:13)验证时(C:\用户\滑动\桌面\注册表后端\差旅管理系统\节点模块\mongoose\lib\架构类型. js:1253:7验证时(C:\用户\滑动\桌面\注册表后端\差旅管理系统\节点模块\mongoose\lib\架构类型. js:1198:14)验证时(C:\用户\滑动\桌面\注册表后端\差旅管理系统\节点模块\mongoose\lib\架构类型. js:2545:18)验证时(C:\用户\滑动\桌面\注册表后端\差旅管理系统\节点模块\mongoose\lib\文档. js:2545:18)验证时(节点:内部/进程/任务队列:78:11){属性:【对象】,种类:"必需",路径:'名称',值:未定义,原因:未定义,[符号(mongoose:验证器错误)]:真}},消息:'寄存器验证失败'}
我从 Postman 那里测试API,得到这个错误
"错误":{"数量":{"名称":"验证器错误","消息":"需要路径Number
。","属性":{"留言":"需要路径Number
。","类型":"必需","路径":"数量"},"种类":"必需","路径":"编号"},"密码":{"名称":"验证器错误","消息":"需要路径Password
。","属性":{"留言":"需要路径Password
。","类型":"必需","路径":"密码"},"种类":"必需","路径":"密码"},"电子邮件":{"名称":"验证器错误","消息":"需要路径Email
。","属性":{"留言":"需要路径Email
。","类型":"必需","路径":"电子邮件"},"种类":"必需","路径":"电子邮件"},"姓名":{"名称":"验证器错误","消息":"需要路径Name
。","属性":{"留言":"需要路径Name
。","类型":"必需","路径":"名称"},"种类":"必需","路径":"姓名"}},"_消息":"寄存器验证失败","名称":"验证错误","消息":"寄存器验证失败:编号:需要路径Number
。,密码:需要路径Password
。,电子邮件:需要路径Email
。,名称:需要路径Name
。"}
3条答案
按热度按时间rqmkfv5c1#
在“NewAdd”和“RegSchema”中使用相同的大小写。
以及
zujrkrfu2#
我遇到了同样的问题。我在服务器运行时更改了架构。我只是重新启动服务器,错误就消失了。如果您在服务器仍在运行时更改了架构,请尝试重新启动服务器。
cxfofazt3#
跟进并需要检查这一点
1.选择postman
下拉菜单上的JSON选项