Currently, I am in the process of building a Vue application using Mongo and Express. Unfortunately, I have encountered a bug after sending a post request to Mongo.
Uncaught (in promise) Error: Request failed with status code 500
at createError (createError.js?2d83:16)
at settle (settle.js?467f:18)
at XMLHttpRequest.handleLoad (xhr.js?b50d:77)
Despite the bug, I am still able to make the post request. However, there is no redirection happening. I have to manually change the route on my frontend app in order to view the posted data. It seems like the issue lies within my post route setup and mongoose model.
Post route:
router.route('/').post(function (req, res) {
let post = new Post(req.body);
post.save()
post.comments.push(Comment)
.then(() => {
res.status(200).json({});
})
.catch(() => {
res.status(400).send("unable to save to database");
})
})
Post model :
const Post = new Schema({
title: {
type: String
},
text: {
type: String
},
img: String,
price: Number,
postType: String,
createdAt: { type: Date, default: Date.now },
comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }]
},{
collection: 'posts'
});
Comment model:
const CommentSchema = new Schema({
text: String,
rating: Number,
author: String,
});
Apologies for any language barriers. I hope my explanation makes sense.