Reputation: 11
I am taking course online and when I implemented a code it gaves me path node found error. I changed my code to exact code in the tutorial but it still giving error. Below is the implemented code and the snippets of error I am facing:
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)
When I call this api using postman with following data:
{
"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
}
It gaves me following error
{
"success": false,
"error": "Bootcamp validation failed: address: Path `address` is required., description: Path `description` is required., name: Path `name` is required."
}
Following is the display of postman where I am calling my api:
Can anyone find out where I am lacking?
Upvotes: 0
Views: 726
Reputation: 1
Make sure you are calling parser middleware before routes.
const app = express();
app.use(express.json());
// Mounted routes
app.use("/api/v1/bootcamps", bootcamps);
Upvotes: 0
Reputation: 1
well ,I‘m face the same problem , you just put the controller
the instance be h the request method
Upvotes: 0
Reputation: 11
Issue with this code is that the req.body is undefined that's why mongoose is raising an error.
The reason for req.body to be undefined is because most probably app.use(express.json()) haven't used in the server.js/index.js file.
Adding app.use(express.json()) in the server file will resolve this error.
Upvotes: 0