首页 > 解决方案 > 尽管昨天有效,但无法发布数据

问题描述

昨天我做了一个简单的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 上有一个类似的问题。我尝试了这个解决方案,但它对我不起作用。

标签: node.jsexpress

解决方案


推荐阅读