无法读取属性' name'在Node JS中未定义

时间:2017-08-18 10:37:35

标签: mysql node.js express ejs

我是Node js的新手。我试图使用mysql在节点js中创建CRUD应用程序。但我面临着我无法理解或处理的错误。请帮忙。和谢谢你在adv。

我无法从ejs页面读取输入字段值到server.js页面。

我的server.js文件在下面给出

        // server.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mysql = require('mysql');
var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "tuts_rest"
});

// set the view engine to ejs
app.set('view engine', 'ejs');

// about page 
app.get('/about', function(req, res) {
    res.render('pages/about');
});

app.post('/insert', function(req,res){

    con.connect();

    console.log(req.body.name);

    con.query("INSERT INTO users(name,email,country) VALUES('"+req.body.name+"','"+req.body.email+"','"+req.body.country+"')", function(err, results) {
      if (!err)
        res.send('Inserted Successfully!')
      else
        throw error;
    });

    con.end();
});

app.listen(8080);
console.log('8080 is the magic port');

我使用表单

发送以下数据
<form role="form" action="/insert" method="POST">
    <div class="form-group">
        <label for="lname">Name:</label>
        <input type="text" class="form-control" id="name" name="name" required>
    </div>
    <div class="form-group">
        <label for="email">Email address:</label>
        <input type="email" class="form-control" id="email" name="email" required>
    </div>
    <div class="form-group">
        <label for="country">Country:</label>
        <input type="text" class="form-control" id="country" name="country">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

和错误如下所示

TypeError: Cannot read property 'name' of undefined
    at E:\xampp\htdocs\node\basic\server.js:40:22
    at Layer.handle [as handle_request] (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\layer.js:95:5)
    at E:\xampp\htdocs\node\basic\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\index.js:335:12)
    at next (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\index.js:275:10)
    at expressInit (E:\xampp\htdocs\node\basic\node_modules\express\lib\middleware\init.js:40:5)
    at Layer.handle [as handle_request] (E:\xampp\htdocs\node\basic\node_modules\express\lib\router\layer.js:95:5)

1 个答案:

答案 0 :(得分:3)

要求body-parser不做任何事情。 你需要让express实际使用它。

// accept url encoded
app.use(bodyParser.urlencoded({
    extended: true
}));

// accept json 
app.use(bodyParser.json());

注意:

  • 您的查询广泛用于sql注入。使用预准备语句或使用knex之类的查询构建器为您执行此操作。

  • 不是逐个创建和管理连接,而是使用连接池。为此,请使用mysql.createPool代替mysql.createConnection