Error : Cannot read properties of undefined

Error : Cannot read properties of undefined

这是我的错误!

TypeError: 无法读取未定义的属性(读取 'newTask') 在 C:\Users\pruth\Desktop\ToDoList\app.js:36:22 在 Layer.handle [作为 handle_request] (C:\Users\pruth\Desktop\ToDoList\node_modules\express\lib\router\layer.js:95:5) 接下来 (C:\Users\pruth\Desktop\ToDoList\node_modules\express\lib\router\route.js:144:13) 在 Route.dispatch(C:\Users\pruth\Desktop\ToDoList\node_modules\express\lib\router\route.js:114:3) 在 Layer.handle [作为 handle_request] (C:\Users\pruth\Desktop\ToDoList\node_modules\express\lib\router\layer.js:95:5) 在 C:\Users\pruth\Desktop\ToDoList\node_modules\express\lib\router\index.js:284:15 在 Function.process_params(C:\Users\pruth\Desktop\ToDoList\node_modules\express\lib\router\index.js:346:12) 接下来 (C:\Users\pruth\Desktop\ToDoList\node_modules\express\lib\router\index.js:280:10) 在 serveStatic (C:\Users\pruth\Desktop\ToDoList\node_modules\serve-static\index.js:75:16) 在 Layer.handle [作为 handle_request] (C:\Users\pruth\Desktop\ToDoList\node_modules\express\lib\router\layer.js:95:5)

这是我的 javascript 代码。

const express = require("express");
const bodyParser = require("body-parser");


const app = express();

//variables
var items = [];

// set up view engine
app.set('view engine' , "ejs");

// set up BodyParser Middleware
app.use(bodyParser.urlencoded({ extended: true }));

// set up public files
app.use( express.static('public'));

app.get("/", function (req, res) {
  var today = new Date();

  var options = {
    weekday: "long",
    day: "numeric",
    month: "long"
  };

  var day = today.toLocaleDateString("en-US", options);
  
  res.render("list" , {ejsday : day, ejsitem : items});
  
});

app.post("/", function (res,req) {

 var item = req.body.newTask;
 items.push(item);
 res.redirect("/");

});

app.listen(3000, function () {
  console.log("Server started on port 3000.");
});

这是我的 EJS 代码

        <ul>
           <li><%=ejsitem%></li>
        </ul>

        <form action="/" method="post">

             <input type="text" name="newTask">
             <button type="submit" name="button">Add</button>
        </form>

请帮忙!

您应该将 app.post("/", function (res,req) { 更改为 app.post("/", function (req,res) {

编辑:添加一些细节

根据API reference,第一个参数总是包含请求req,然后是响应res

因此将第一个参数 res 更改为 req