我正在在线学习课程,当我实现代码时,它给了我路径节点发现错误。我将代码更改为教程中的确切代码,但仍然出现错误。下面是实现的代码和我面临的错误片段:
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)
当我使用邮递员使用以下数据调用此 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": "[email protected]",
"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 的显示:
谁能找出我的不足吗?
此代码的问题是 req.body 未定义,这就是 mongoose 引发错误的原因。
req.body 未定义的原因是很可能 app.use(express.json()) 尚未在 server.js/index.js 文件中使用。
在服务器文件中添加 app.use(express.json()) 将解决此错误。
好吧,我也面临着同样的问题,你只要把控制器放上即可
实例是请求方法
确保在路由之前调用解析器中间件。
const app = express();
app.use(express.json());
// Mounted routes
app.use("/api/v1/bootcamps", bootcamps);