【发布时间】:2018-07-18 06:38:42
【问题描述】:
我正在尝试使用下面的 post 方法来创建新文档。但是当我在 Postman 中发送一个帖子请求(例如http://localhost:3000/api/posts?title=HeaderThree)时,会创建一个新文档,但缺少键和值。
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!'});
});
});
架构是这样的:
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);
【问题讨论】:
标签: javascript node.js mongodb express mongoose