1
votes

I am a newbie programmer. I've been making a very small website. I met little error. I've found some solutions. But I haven't had an answer yet.

If you don't mind, Please give me your touhgt. T^T

I connected node.js with Mysql. 'GET' is running without any problem. but 'POST' section can't get some data which I type on the webpage.

The thing I wonder is that why 'POST' don't get data.

var fs = require('fs');
var ejs = require('ejs');
var http = require('http');
var mysql = require('mysql');
var express = require('express');

var client = mysql.createConnection({
  user: 'root',
  password: 'PASSWORD',
  database: 'Company'
});

var app = express();

http.createServer(app).listen(8080, function(){
  console.log('Server running at http://127.0.0.1:8080');
});

app.get('/', function(request, response) {
  fs.readFile('list.html', 'utf8', function(error, data) {
    client.query('SELECT * FROM products', function (error, results) {
       response.send(ejs.render(data, {
        data: results
      }));
    });
  });
});


app.get('/delete/:id', function(request, response) { 
  client.query('DELETE FROM products WHERE id=?', [request.param('id')], function() {
    response.redirect('/');
  });
});

app.get('/insert', function(request, response) { 
  fs.readFile('insert.html', 'utf8', function (error, data) {
  response.send(data);
  });
}); 

app.post('/insert', function(request, response) {
  var body = request.body;

  client.query('INSERT INTO products (name, modelnumber, series) VALUES (?, ?, ?)'[
    body.name, body.modelnumber, body.series
    ], function() {
      response.redirect('/');
  });
});

app.get('/edit/:id', function(request, response) { 
  fs.readFile('edit.html', 'utf8', function(error, data) {
    client.query('SELECT * FROM products WHERE id = ?', [
      request.param('id')
    ], function(error, result) {
      response.send(ejs.render(data, {
        data: result[0]
      }));
    });
  });
});

app.post('/edit/:id', function(request, response) { 
  var body = request.body;

  client.query('UPDATE products SET name=?, modelnumber=?, series=? WHERE id=?', [
    body.name, body.modelnumber, body.series, request.param('id')
    ], function() {
      response.redirect('/');
    });
});

The error message

TypeError: Cannot read property 'name' of undefined at app.get.fs.readFile.client.query.response.send.ejs.render.data (/home/han/app.js:46:9) at callbacks (/home/han/node_modules/express/lib/router/index.js:164:37) at param (/home/han/node_modules/express/lib/router/index.js:138:11) at pass (/home/han/node_modules/express/lib/router/index.js:145:5) at Router._dispatch (/home/han/node_modules/express/lib/router/index.js:173:5) at Object.router (/home/han/node_modules/express/lib/router/index.js:33:10) at next (/home/han/node_modules/express/node_modules/connect/lib/proto.js:193:15) at Object.expressInit [as handle] (/home/han/node_modules/express/lib/middleware.js:30:5) at next (/home/han/node_modules/express/node_modules/connect/lib/proto.js:193:15) at Object.query [as handle] (/home/han/node_modules/express/node_modules/connect/lib/middleware/query.js:45:5)

When I tried to change array values to 'value' app.post('/insert') section, the webpage showed 'value'.

2

2 Answers

1
votes

The error is that request.body is undefined. It is undefined, because you have not configured Express to parse the body. To do this, you need to install the module body-parser

npm install body-parser

Next, require body-parse in your code

var bodyParser = require('body-parser')

And set it as middleware after you initialized Express.

var app = express();
app.use(bodyParser.urlencoded({ extended: false }));

Your request.body should now equal to the form.

EDIT1:

Try this for your insert query

app.post('/insert', function(request, response) {
  var body = request.body;

  var product = {
    name: body.name,
    modelnumber: body.modelnumber,
    series: body.series
  }
  client.query('INSERT INTO products SET ?', product, function() {
      response.redirect('/');
  });
});
0
votes

In this section:

app.post('/insert', function(request, response) {
  var body = request.body;

  client.query('INSERT INTO products (name, modelnumber, series) VALUES (?, ?, ?)'[
    body.name, body.modelnumber, body.series
    ], function() {
      response.redirect('/');
  });
});

Try to replace usage of request.body with request.params. when using forms + post, node will interpret the fields as params on the request object.