I am experiencing an issue with my simple Post model and route for creating posts. After submitting a new post using Postman, the request hangs for a moment before returning an error in JSON format. The model data is never saved successfully.
Below is the structure of my model:
const PostSchema = mongoose.Schema({
title: {
type: String,
required: true
},
body: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
And here is the code snippet for my route:
router.post('/', (req, res) => {
console.log(req.body.title);
console.log(req.body.body);
const post = new Post({
title: req.body.title,
body: req.body.body
});
post.save()
.then(data => {
console.log(data);
res.json(post);
})
.catch(err => {
res.json({
error: err
});
});
});
I have ensured that all necessary modules are imported correctly. Any assistance would be greatly appreciated!