0

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);
chridam
  • 100,957
  • 23
  • 236
  • 235
SJC
  • 107
  • 2
  • 12

2 Answers2

1

If you are sending data as query string:

 http://localhost:3000/api/posts?title=HeaderThree
                                 ^^^^^^^^^^^^^^^^^

Then you have to use req.query to get them from express server:

const post = new Post(
    {
        title: req.query.title,
        text: req.query.text
    }
);

If you want to send data as form bodies, you need to use body-parser middleware:

const bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

...

Now you could use req.body object to get submitted data.

One last thing, don't forget to populate form data under body tab in postman

YouneL
  • 8,152
  • 2
  • 28
  • 50
  • I changed body to queries and now it works perfectly. I must have made a mistake somewhere else though, because I had already included app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); – SJC Feb 07 '18 at 21:03
0

Try to add :

  _id: mongoose.Schema.Types.ObjectId,

in your model :

const PostSchema = new Schema(
    {   
        _id: mongoose.Schema.Types.ObjectId,
        date: {type: Date, default: Date.now},
        title: {type: String},
        text: {type: String },
        comments: {type: Array}
    }
)

before you insert the document you have to give it an _id :

_id : mongoose.Types.ObjectId()

your document will be :

const post = new Post(
            {
                _id :  mongoose.Types.ObjectId(),
                title: req.body.title,
                text: req.body.text
            }
        );

check this post

M. Gara
  • 1,023
  • 2
  • 14
  • 27
  • I tried this and it returns { "message": "document must have an _id before saving", "name": "MongooseError" } – SJC Feb 07 '18 at 20:49
  • Now it's going back to what it did before. It creates a document with the default date and an id but doesn't include the req.body values. – SJC Feb 07 '18 at 20:59