I'm trying to use the post method below to create new documents. But when I send a post request in Postman (e.g. http://localhost:3000/api/posts?title=HeaderThree) a new document is created but the keys and values are missing.
router.route('/posts')
.get(function(req, res) {
Post.find(function(err, posts) {
if (err) { return res.send(err)}
res.json(posts)
})
})
.post(function(req, res) {
const post = new Post(
{
title: req.body.title,
text: req.body.text
}
);
post.save(function(err, post) {
if (err) { return res.send(err)};
res.json({ message: 'Post added!'});
});
});
The schema is this:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const PostSchema = new Schema(
{
date: {type: Date, default: Date.now},
title: {type: String},
text: {type: String },
comments: {type: Array}
}
)
module.exports = mongoose.model('PostSchema', PostSchema);