简体   繁体   English

TypeError:无法读取未定义的属性“名称”。 我找不到出现此错误的原因。 我使用 mysql、nodejs 和 ejs 作为视图引擎

[英]TypeError: Cannot read property 'name' of undefined. I cannot find the reason why I get this error. I am using mysql, nodejs and ejs as view engine

I am Trying to Add new users into a MySql database in nodejs and EJS as a view engine.我正在尝试将新用户添加到 nodejs 和 EJS 中的 MySql 数据库中作为视图引擎。 I do get a TypeError: Cannot read property 'name' of undefined error.我确实收到 TypeError: Cannot read property 'name' of undefined 错误。 Can somebody please assist as I cannot find the reason why I get this error.有人可以帮忙,因为我找不到我收到此错误的原因。

 My Routing const express = require("express"); var router = express.Router(); const mysql = require("mysql"); const mysqlConnection = require("../connection"); router.get("/", function (req, res) { let mysql = "SELECT * FROM user_index.clients"; let query = mysqlConnection.query(mysql, function (err, rows) { if (err) { throw err; } else { res.render("users", { title: "Mysql with EJS", users: rows, }); } }); }); router.get("/add", function (req, res) { // res.send("My add page."); res.render("add", { title: "Mysql with EJS", }); }); router.post("/add", (req, res) => { let data = { name: req.body.name, email: req.body.email, phone_number: req.body.phone_number, }; let mysql = "INSERT INTO clients SET?"; let query = mysqlConnection.query(mysql, data, function (err, result) { if (err) { throw err; } else { res.redirect("/"); } }); }); module.exports = router;

Since you're submitting a html-form you need a corresponding parser in order to be able to access the submitted data under req.body .由于您提交的是html-form ,因此您需要相应的解析器才能访问req.body下提交的数据。 Adding the following middleware before your handlers should fix the problem:在您的处理程序之前添加以下中间件应该可以解决问题:

const bodyParser = require('body-parser');
...
app.use(bodyParser.urlencoded({ extended: false })) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行我的nodejs代码时收到此错误:'TypeError:无法读取未定义的属性'apply'。 ' - I get this error when running my nodejs code: ' TypeError: Cannot read property 'apply' of undefined. ' TypeError:无法读取未定义的属性'addTopic'。 我是盲人吗? - TypeError: Cannot read property 'addTopic' of undefined. Am I blind? 类型错误:无法读取未定义的属性“then”。 当我尝试运行 updatefirst 函数时出现此错误 - TypeError: Cannot read property 'then' of undefined. Getting this error when I am trying to run updatefirst function 我目前正在学习反应,我遇到了错误。 TypeError:无法读取未定义的属性“图像” - I am currently learning react and I ran into error. TypeError: Cannot read property 'image' of undefined 无法读取未定义的属性“插入”。 我发布不正确吗? - Cannot read property 'insert' of undefined. Am I not publishing correctly? 我正在尝试从数据库中获取 map mydata 但我收到此错误。 TypeError:无法读取未定义的属性“地图” - I am trying to map mydata from the database but i am getting this error. TypeError: Cannot read property 'map' of undefined 错误类型错误:无法读取未定义的属性“过滤器”。 我该如何解决? - ERROR TypeError: Cannot read property 'filter' of undefined. How do I fix this? 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我收到 TypeError:无法读取未定义的属性“执行” - Why am I receiving TypeError: cannot read property 'execute' of undefined 我找不到原因“错误:无法读取未定义的属性 'toString'” - I can not find the reason “ERROR: Cannot read property ‘toString’ of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM