“类型错误:无法读取未定义的属性(读取‘puttit’)”

问题描述 投票:0回答:1

所以。我正在做一个网络课程的顶点项目。由于某种原因,每当我提交表单时,它都会向我抛出这个问题(出于隐私考虑,我编辑的所有内容都将我的真实姓名更改为“siffrin”。)

TypeError:无法读取未定义的属性(读取“puttit”) 在文件:///Users/siffrin/Downloads/coursework/writing%20stuff/bablog%20(capstone%20project)/index.js:17:25 在Layer.handle [作为handle_request](/Users/siffrin/Downloads/coursework/writing stuff/bablog(顶点项目)/node_modules/express/lib/router/layer.js:95:5) 接下来(/Users/siffrin/Downloads/coursework/writing stuff/bablog(顶点项目)/node_modules/express/lib/router/route.js:149:13) 在Route.dispatch(/Users/siffrin/Downloads/coursework/writing stuff/bablog(顶点项目)/node_modules/express/lib/router/route.js:119:3) 在Layer.handle [作为handle_request](/Users/siffrin/Downloads/coursework/writing stuff/bablog(顶点项目)/node_modules/express/lib/router/layer.js:95:5) 在/Users/siffrin/Downloads/coursework/writing stuff/bablog(顶点项目)/node_modules/express/lib/router/index.js:284:15 在 Function.process_params (/Users/siffrin/Downloads/coursework/writing stuff/bablog(顶点项目)/node_modules/express/lib/router/index.js:346:12) 接下来(/Users/siffrin/Downloads/coursework/writing stuff/bablog(顶点项目)/node_modules/express/lib/router/index.js:280:10) 在/Users/siffrin/Downloads/coursework/writing stuff/bablog(顶点项目)/node_modules/body-parser/lib/read.js:137:5 在 AsyncResource.runInAsyncScope (节点:async_hooks:211:14)

我不知道为什么要这样做。我尝试过更改“标题”中的名称,并且尝试查找它,但从我发现的情况来看,我认为我无法以此处适用的方式应用它

这是代码 - 我在这方面仍然是一个初学者,有点(虽然我在高中时确实学过 JS,哈哈)所以请原谅我的任何新手错误。我想为其余的帖子创建一个 for 循环(并清理那些占位符),但考虑到我无法让 The Very First 帖子正常工作...... 电子书

<head>
  <title>Bablog!:The Baba Blogging Platform!</title>
  <link rel="stylesheet" href="/style.css" />
</head>
<body>
  <div><img id="logo" src="/bablog.png" /></div>
  <div id="box">
    <div id="welcome">
      <p>Welcome to the <b>BABLOG!!!</b></p>
    </div>
    <% if (locals.ptit) {%>
    <div class="post">
        <h1><%= ptit[0] %></h1>
        <p><%= stuff[0] %></p>
    <form action="/delete" method="DELETE">
        <input type="submit" value="POST IS DONE" class="donebtn"/>

    </form></div>
    <% } %>
    <div id="place">
      <form action="/submit" method="POST">
        <label for="puttit"><input type="text" id="puttit" name="puttit" placeholder="title here"
            /></label>
        <br /><br />

        <label for="posty"><textarea
            name="posty"
            placeholder="Bablog your post!"
            rows="10"
            cols="30"
          ></textarea></label>
        
        <br /><br />

        <input type="submit" value="Submit" />
      </form>
    </div>
    <div class="post"><p>I'm a baba!</p></div>
    <div class="post"><p>I'm a baba!</p></div>
    <div class="post"><p>I'm a baba!</p></div>
    <div class="post"><p>I'm a baba!</p></div>
  </div>
</body>

js

import express from "express";
import bodyParser from "body-parser";

const app = express();
const port = 3000;
var titles = [];
var posts = [];

app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }))


app.get("/", (res, req) => {
  req.render("blog.ejs");
});
app.post("/submit", (res, req) => {
    titles.push(req.body["puttit"]);
    posts.push(req.body["posty"]);
  req.render("blog.ejs", {
    ptit: titles,
    stuff: posts,
  });
});

app.listen(port, () => {
  console.log(`Port: ${port}`);
});

css 如果你需要的话

* {
    background-color: black;
    color:white;
};
#logo {
}
#box {
    display: grid;
    grid-template-columns: 2fr 3fr;
    grid-template-rows: 2fr 3fr;
    row-gap: 5px;

}
#welcome {

}
.post {
    background-color: #d9396a;
    border: outset black double
}
.post > *{
    background-color: #d9396a;
    color: black

}
#place {
    grid-row: 2 / span 10;
}
input, textarea {
    background-color: #82261c;
    color: black
}
#place > * {
    border: outset white double;

}
.donebtn {
    float:right;
    background-color: white;
}```
forms express ejs body-parser
1个回答
0
投票

对于 API 方法,正确的参数顺序是 (req, res),而不是 (res, req)。 Express.js 文档中的示例:

admin.get('/', function (req, res) {
  console.log(admin.mountpath) // /admin
  res.send('Admin Homepage')
})
© www.soinside.com 2019 - 2024. All rights reserved.