node.js - 尽管昨天有效,但无法发布数据
问题描述
昨天我做了一个简单的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 上有一个类似的问题。我尝试了这个解决方案,但它对我不起作用。
解决方案
推荐阅读
- r - 如何标记来自连续条形值的 y 轴列值的总和,例如每个 x 轴“日期”的“已确认”案例
- hana - HANA Studio - 计算视图计算的列未正确聚合
- java - @Transactional 不适用于多个存储库调用
- java - Cytoscape 安装和执行
- mysql - 删除 MySQL 和 PostgreSQL 的查询区别
- typescript - 高阶函数返回类型 void 被忽略
- javascript - 如何使用 JavaScript 检测图像是否被粘贴或拖放到内容可编辑区域?
- python - 访问列表索引中字符串中的每个字符
- sql-server - 如何将 Laravel 7 连接到 Microsoft SQL Server?
- swift - How can I read text from a file inside the bundle in Swift?