gpt4 book ai didi

javascript - 验证错误mongodb,node.js和Express后端

转载 作者:太空宇宙 更新时间:2023-11-04 01:22:24 26 4
gpt4 key购买 nike

我正在尝试通过邮递员发送发帖请求,以向我的请假架构添加新的请假类型。但是,当我发送发布请求时,出现验证错误,通知我用户对象中每个属性都需要一个“路径”。确切的错误说:“ _message”:“离开验证失败”,
    “ message”:“离开验证失败:类型:路径type是必需的。颜色:路径color是必需的。isEnabled:路径isEnabled是必需的。”,
    “ name”:“ ValidationError”

***Route files***
const express = require('express')
const router = express.Router()
var bodyParser = require ('body-parser')
// Require the controllers WHICH WE DID NOT CREATE YET!!
const LeaveSchema = require('../models/leaveType')
// a simple test url to check that all of our files are communicating correctly.7
//router.use(bodyParser());
// create a leaveType
// router.post('/api/leaveTypes/makeLeaveType/', (req, res) => {
// LeaveSchema.create(req.body)
// .then(leaveTypes => res.json(leaveTypes))
// })
router.use(bodyParser());
// get aLL leaveTypes
router.get('/api/leaveTypes', (req, res) => {
LeaveSchema.find(function (err, leave) {
if (err) {
console.log(err)
} else {
res.json(leave)
}
})
})

router.get('/api/leaveTypes/:id', (req, res) =>
LeaveSchema.find({ _id: req.params.id })
.then(function (leaveTypes) {
res.json(leaveTypes)
})
.catch(err => console.log(err)))

router.put('/api/leaveTypes/:id', async function (req, res, next) {
var x = await LeaveSchema.find({ _id: req.params.id })

console.log(x[0].isEnabled)

if (x[0].isEnabled === true) {
LeaveSchema.findById(req.params.id).updateOne({ $set: { isEnabled: false } },
function (err, users) {
if (err) {
console.log(err)
} else {
res.json(users)
}
}) } else {
LeaveSchema.findById(req.params.id).updateOne({$set: { isEnabled: true } },
function (err, users) {
if (err) {
console.log(err)
} else {
res.json(users)
}})}
})
router.delete('api/leavetypes/delete/:id', function(req,res){
var id = req.params.id;
LeaveSchema.findOneAndRemove({_id: id}, function(err){
if(err){
console.log(err);
return res.status(500).send();
}
return res.status(200).send();
});
});
router.post('/api/leavetypes/add', function(req, res) {

const type = req.body.type;
const color = req.body.color;
const isEnabled = req.body.isEnabled;

const newLeaveType = new LeaveSchema({
type,
color,
isEnabled
});

newLeaveType.save()
.then(()=> res.json('New Leave Added!'))
.catch(err => res.send(err));


});

module.exports = router










***Leave Types Model***

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const LeaveSchema = new Schema({
type: { type: String, required: true, max: 100 },
color: { type: String, required: true, max: 100 },
isEnabled: { type: Boolean, required: true, max: 100 }

})

// Export the model`enter code here`
module.exports = mongoose.model('Leave', LeaveSchema)

最佳答案

代码没有问题,问题可能出在您发送请求的方式上。尝试像这样发送请求。

enter image description here

关于javascript - 验证错误mongodb,node.js和Express后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58803904/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com