【发布时间】:2023-04-06 09:14:01
【问题描述】:
这是我的路由器
router.post('/', async(req, res) => {
const posts = await loadPostsCollection()
await posts.insertOne({
text: req.body.text,
createdAt: new Date()
})
res.status(201).send()
})
这是终端错误
在端口 3090 上监听 ..... (node:7764) UnhandledPromiseRejectionWarning:TypeError:无法读取属性 未定义的“文本” 在 /Users/macbook/Desktop/node-project/server/routes/api/posts.js:19:24 在 processTicksAndRejections (internal/process/task_queues.js:94:5) (node:7764) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个 错误源于在异步函数内部抛出 没有 catch 块,或拒绝未处理的承诺 使用 .catch()。 (拒绝 ID:1)(节点:7764)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。在 未来,未处理的承诺拒绝将终止 具有非零退出代码的 Node.js 进程。
【问题讨论】:
-
console.log(req.body) 给出了什么?看来你的第二个错误,你应该使用 trycatch 语句。
标签: javascript node.js express