猫鼬:验证失败:地址:路径'地址'是必需的,描述:路径'描述'是必需的,名称:路径'名称'是必需的。



我正在网上学习课程,当我实现一个代码时,它给了我路径节点发现错误。我改变了我的代码,以准确的代码在教程中,但它仍然给出错误。下面是实现的代码和我所面临的错误片段:

bootcampModel.js

const mongoose = require('mongoose')
const bootcampSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Please enter a name'],
unique: true,
trim: true,
maxlength: [50, 'Name cannot be more than 50 characters'],
},
slug: String,
description: {
type: String,
required: [true, 'Please enter description'],
maxlength: [500, 'Description cannot be more than 500 characters'],
},
website: {
type: String,
match: [
/https?://(www.)?[-a-zA-Z0-9@:%._+~#=]{1,256}.[a-zA-Z0-9()]{1,6}b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/,
'Please enter a valid URL with HTTP or HTTPS',
],
},
phone: {
type: String,
maxlength: [20, 'Phone number cannot exceed 20 characters'],
},
email: {
type: String,
match: [
/^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/,
'Please enter valid email',
],
},
address: {
type: String,
required: [true, 'Please enter address'],
},
location: {
type: {
type: String,
enum: ['Point'],
},
coordinates: {
type: [Number],
index: '2dsphere',
},
formattedAddress: String,
street: String,
city: String,
state: String,
zipcode: String,
country: String,
},
careers: {
type: [String],
required: true,
enum: [
'Web Development',
'Mobile Development',
'UI/UX',
'Data Science',
'Other',
],
},
averageRating: {
type: Number,
min: [1, 'Rating must be atleast 1'],
max: [10, 'Rating cannot exceed 10'],
},
averageCost: Number,
photo: {
type: String,
default: 'no-photo.jpg',
},
housing: {
type: Boolean,
default: false,
},
jobAssistance: {
type: Boolean,
default: false,
},
jobGuarrantee: {
type: Boolean,
default: false,
},
acceptGi: {
type: Boolean,
default: false,
},
createdAt: {
type: Date,
default: Date.now(),
},
})
module.exports = mongoose.model('Bootcamp', bootcampSchema)

bootcampController.js

exports.createBootcamp = async (req, res, next) => {
try {
const bootcamp = await Bootcamp.create(req.body)
res.status(201).json({ success: true, data: bootcamp })
} catch (err) {
res.status(400).json({ success: false, error: err.message })
}
}

routes.js

router.route('/').post(createBootcamp)

当我使用以下数据使用postman调用这个api时:

{
"name": "Devcentral Bootcamp",
"description": "Is coding your passion? Codemasters will give you the skills and the tools to become the best developer possible. We specialize in front end and full stack web development",
"website": "https://devcentral.com",
"phone": "(444) 444-4444",
"email": "enroll@devcentral.com",
"address": "45 Upper College Rd Kingston RI 02881",
"careers": [
"Mobile Development",
"Web Development",
"Data Science",
"Business"
],
"housing": false,
"jobAssistance": true,
"jobGuarantee": true,
"acceptGi": true
}

显示如下错误

{
"success": false,
"error": "Bootcamp validation failed: address: Path `address` is required., description: Path `description` is required., name: Path `name` is required."
}

下面是我调用api的邮差显示:

邮差

谁能找出我缺少的地方?

这段代码的问题是:Body未定义,这就是mongoose抛出错误的原因。

请求的原因。Body未定义的原因很可能是app.use(express.json())没有在server.js/index.js文件中使用。

添加app.use (express.json ())在服务器文件中将解决此错误。

我遇到了同样的问题,你只要把控制器
实例放到请求方法

相关内容

最新更新