TypeError: Cannot read property &#39;message&#39; of undefined<br> &nbsp; &nbsp;

TypeError: Cannot read property &#39;message&#39; of undefined<br> &nbsp; &nbsp;

我在 Postman post 请求 express 服务器代码时收到 TypeError: Cannot read property &#39;message&#39; of undefined<br> &nbsp; &nbsp; 错误。

Server.js

const express = require('express');
const {v4: uuidv4} = require('uuid');
const app = express();

// Middleware for data on post request
app.use(express.json({extended: false}));


// Data
const todos = [
    {
        message: "Buy tea", 
        id: 1
    }, 
    {
        message: "Buy wrp for lunch",
        id: 2
    },
    {
        message: "Play game",
        id: 3
    }
];

/* --------- API ---------- */
app.get("/", (req, res) => {
    res.status(200).json(todos);
})

app.post("/", (req, res) => {
    const newtodo = {
        message: res.body.message,
        id: uuidv4()
    }
    todos.push(newtodo);
    res.status(201).json(todos);
})



const PORT = process.env.PORT || 5001;
app.listen(PORT, () => {
    console.log(`Server running on port: ${PORT}`);
} );

Headers 我有 Key Content-type Value application/json。但我一直收到错误。截图如下:

似乎无法查明问题所在。 Message 在我的 server.js 文件中定义。

我觉得这里应该是req.body.message而不是res.body.message

app.post("/", (req, res) => {
const newtodo = {
    message: req.body.message,
    id: uuidv4()
}
todos.push(newtodo);
res.status(201).json(todos);
})

如果我错了,你可以像这样使用可选链接 res?.body?.message

Javascript Optional Chaining