当前位置: 首页 > 知识库问答 >
问题:

TypeError:无法读取未定义的属性“msg”

井昊乾
2023-03-14

app.js

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

const app = express();

app.set(bodyParser.urlencoded({extended:false}));
app.set('view engine', 'ejs');
app.use(express.static('public'));

var blogName = "";

app.get("/", function(req, res){
    blogName = "Priyanshu Gupta's Not-So-Secret Diary";
    res.render("index", {titleName : blogName});
})
app.get("/about", function(req, res){
    blogName = "About This Blog";
    res.render("about", {titleName : blogName});
})
app.get("/contact", function(req, res){
    blogName = "Contact Me";
    res.render("contact", {titleName : blogName});
})

app.get("/blog", function(req, res){
    blogName = "Blog Post";
    res.render("blogPage", {titleName : blogName})
})

app.get("/compose", function(req, res){
    res.render("compose")
})

app.post("/contact", (req, res) => {
    console.log(req.body.msg);
})

app.listen(3000, function(){
    console.log("Your server is running on port 3000!");
})

我在代码中使用了express,得到的错误如下

TypeError:无法读取未定义的属性'msg'at F:\Priyansh\Coding Playground\Web Development\Blog\app.js:36: 26 at Layer.handle[作为handle_request](F:\Priyantu\Coding Playground\Web Development\Blog\node_modules\Express\lib\router\layer.js:95: 5)在下一个(F:\Priyansh\Coding Playground\Web Development\Blog\node_modules\Express\lib\router\route.js:137: 13)在Route.dispatch(F:\Priyansh\Coding Playground\Web Development\Blog\node_modules\Express\lib\router\route.js:112: 3)在Layer.handle[作为handle_request](F:\Priyansh\Coding Playground\Web Development\Blog\node_modules\Express\lib\router\layer.js:95: 5)at F:\Priyansh\Coding Playground\Web Development\Blog\node_modules\Express\lib\router\index.js:281: 22 at Function.process_params(F:\朴雅树\Coding Playground\Web Development\Blog\node_modules\Express\lib\router\index.js:335: 12)在下一个(F:\朴雅树\Coding Playground\Web Development\Blog\node_modules\Express\lib\router\index.js:275: 10)在serveStatic(F:\朴雅树\Coding Playground\Web Development\Blog\node_modules\服务静态\index.js:75: 16)在Layer.handle[作为handle_request](F:\Priyansh\Coding Playground\Web Development\Blog\node_modules\Express\lib\router\layer.js:95: 5)

联系html

<%- include('header') %>

<div class="container mt-4">
    <h1 style="text-align: center;">Contact Me</h1>
    <form action="/contact" method="POST">
      <div class="mb-3">
        <label for="email" class="form-label">Email address</label>
        <input type="email" name="email" class="form-control" placeholder="name@example.com" autocomplete="off">
      </div>
      <div class="mb-3">
        <label for="msg" class="form-label">Example textarea</label>
        <textarea class="form-control" name="msg" rows="3" style="margin-top: 0px; margin-bottom: 0px; height: 224px;" required></textarea>
      </div>
      <button type="submit" class="btn btn-success mb-4">Submit</button>
    </form>
</div> 
<%- include('footer') %> 

正文解析器无法检测到联系人表单中的msg,请任何人都找出我在这里面临的问题是什么

共有2个答案

苗信鸥
2023-03-14

您忘记了标题application/jsonHTML表单不支持它,因此需要一种解决方法。我建议将其与fetchapi一起使用,然后发布数据或其他信息。如果是这样,我仍然没有100%的信心,但是试着这样做。

刘俊语
2023-03-14

超文本标记语言文件中的数据已成功发送,但app.js无法解析请求正文。为了将数据解析成JSON文件,应该正确使用主体解析器。当服务器文件按结构划分时,每个. js文件都应该使用body解析器。

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
js prettyprint-override">const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");

const app = express();

app.set(bodyParser.urlencoded({extended:false}));
app.set('view engine', 'ejs');

//app.use(express.static('public'));

/// try this
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));

var blogName = "";

app.get("/", function(req, res){
    blogName = "Priyanshu Gupta's Not-So-Secret Diary";
    res.render("index", {titleName : blogName});
})
app.get("/about", function(req, res){
    blogName = "About This Blog";
    res.render("about", {titleName : blogName});
})
app.get("/contact", function(req, res){
    blogName = "Contact Me";
    res.render("contact", {titleName : blogName});
})

app.get("/blog", function(req, res){
    blogName = "Blog Post";
    res.render("blogPage", {titleName : blogName})
})

app.get("/compose", function(req, res){
    res.render("compose")
})

app.post("/contact", (req, res) => {
    console.log(req.body.msg);
})

app.listen(3000, function(){
    console.log("Your server is running on port 3000!");
})
 类似资料:
  • 如果函数在组件中,那么一切都很好。如果我把它单独取出并导出到一个组件中,那么我会得到一个错误TypeError:不能读取未定义的属性(读取'reduce')

  • 问题内容: 我试图在ajax回调从REST api接收数据后设置组件的setState。这是我的组件构造函数代码 然后,我有一个如下所示的方法。 现在,这是我执行getAJAX请求的getPosts函数。 我想设置状态,但出现以下错误。 不太清楚是什么原因造成的。如果有人指出我正确的方向,那将真的很有帮助。提前致谢。 问题答案: 还绑定回调函数,以便回调内部指向React Component的上下

  • 我试图使用node.js express web服务器中的@tensorflow/tfjs-node模块。但是,我得到了下面的错误。我不明白为什么我会出现这个错误。我刚刚在node.js服务器中添加了1行代码。我使用“npm install@tensorflow/tfjs-node”完成的安装。可能的问题是什么? null 先谢谢你, var nonMaxSuppressionV3Impl=tf.

  • 我正在尝试联系许多对许多,在两个模式“培训和培训课程”,所以我做了第三个模式“课程”,以尝试关系1对许多。但是我得到了一个错误: null TypeError:无法读取对象上未定义得属性“belongsto”。(c:UsersDellDownloadsGraphql-12.18.2020Graphql-12.18.2020Graphql-12.18.2020ServerAppDatabaseDat

  • 上面的函数返回一个类似“failed”的字符串。但是,当我尝试在其上运行then函数时,它将返回 并且光标指示在之后和。 下面是完整的功能: 更新 下面是函数 我确信将导致一个“failed”字符串。没别的了。

  • 无法显示Barchart。 我有一个包含数据的表,它是可拖动的。在表格旁边我有一个两个dropbox被拖动的表格中的数据可以被拖动到框中。当将数据拖动到框中时,应该会显示条形图。 这是我的家。component.html 这是我的家。Component.TS 这是我的酒吧 在拖放数据时,特定的图表必须显示在Dropbox上。