尽管昨天可以正常工作但无法发布数据

时间:2019-07-05 08:23:26

标签: node.js express

昨天我制作了一个简单的API,用于测试。在那里,我可以做所有事情,获取,发布,删除和打补丁。但是今天我只能获取和删除。当我要发布帖子时,出现错误,我不理解,因为昨天它很好用。

帖子

帖子1

这是我的帖子:

router.post('/', async (req, res) => {
    const post = new Post({
        title: req.body.title,
        description: req.body.description
    });
    try {
        const savedPost = await post.save();
        res.json(savedPost);
    } catch (err) {
        res.json({
            message: err
        });
    }
});

帖子2

我发表了另一篇文章,但这也行不通:

router.post('/', (req, res) => {
    const postData = {
        title: req.body.title,
        description: req.body.description
    }
    Post.findOne({
        title: req.body.title,
        description: req.body.description
    })
    .then(post => {
        if (!post) {
            Post.create(postData);
            res.json(postData);
        }
    })
    .catch(err => {
        res.send(err);
    })
})

错误

这是我得到的错误:

TypeError: Cannot read property 'title' of undefined
    at router.post (D:\Sonstiges\VUE_JS\rest_api\routes\posts.js:35:25)
    at Layer.handle [as handle_request] (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\layer.js:95:5)
    at D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\index.js:174:3)
    at router (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\index.js:317:13)
    at D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\router\index.js:275:10)
    at expressInit (D:\Sonstiges\VUE_JS\rest_api\node_modules\express\lib\middleware\init.js:40:5)

架构

因此它说标题不确定。在我的模型中,我将其定义为字符串:

const PostSchema = mongoose.Schema({
    title: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        default: Date.now
    }
});

邮递员

在邮递员中,我发布一个带有原始正文且类型为JSON(application / json)的帖子:

{
    "title": "test",
    "description": "this is a test"
}

如何定义标题?

编辑

Stackoverflow也有类似的question。我尝试了此解决方案,但对我而言不起作用。

0 个答案:

没有答案