繁体   English   中英

无法读取Express中的属性

[英]Cannot read property in express

抱歉,知道这样的问题很流行,但是我在那找不到答案。

有输出

无法读取未定义的属性“标题”

链接到此文件中的发布请求

const express = require('express');
const router = express.Router();

router.get('/', function (req, res, next) {
    let query = 'select event.id,   event.title,    event.description,  event.date, place.title as place,   organisator.title as organisator,   subcategory.title as subcategory from ((event inner join place on event.place_id = place.id) inner join organisator on event.organisator_id = organisator.id) inner join subcategory on event.subcategory_id = subcategory.id';
    res.locals.connection.query(query, function (error, results, fields) {
        if (error) throw error;
        res.render('index', { title: 'Events', data: results });
    });
});

router.post('/add', function (err, req, res) {
    const query = "insert into event (title, description, event.date, subcategory_id, place_id, organisator_id) values ('" + req.body.title + "','" + req.body.description + "','" + req.body.date + "',(SELECT id from subcategory WHERE title = '" + req.body.subcategory + "'),(SELECT id from place WHERE title = '" + req.body.place + "'),(SELECT id from organisator WHERE title = '" + req.body.organisator + "'))";
    connection.query(query, function (err) {
        if (err) {
            console.error(err);
            throw err;
        }
    });
});

抱歉滚动,只有这样的查询。 找到了一些答案,该解析器应该在任何东西之前使用,但是我认为我没有这样的问题。 有我的应用程序文件。

const express = require('express');
const path = require('path');
const logger = require('morgan');
const bodyParser = require('body-parser');

var app = express();

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

app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

var mysql = require("mysql");
app.use(function(req, res, next){
    res.locals.connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : 'Strange3865hum31315',
        database : 'minskevents'
    });
    res.locals.connection.connect();
    next();
});

app.use('/', require('./routes/index'));
app.use('/events', require('./routes/events'));

app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

app.use(function(err, req, res, next) {
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  res.status(err.status || 500);
  res.render('error');
});

var http = require('http');

var server = http.createServer(app);
server.listen(4000);
if(server.listening){
  console.log('Server is listening on port 4000');
}

module.exports = app;

我从html表单获取数据

<form action="/events/add" method="POST">
        <input type="text" name="title" placeholder="Event name" />
        <input type="text" name="subcategory" placeholder="Category" />
        <input type="text" name="date" placeholder="Date" />
        <input type="text" name="place" placeholder="Place" />
        <input type="text" name="organisator" placeholder="Organisator" />
        <textarea name="description" placeholder="Description" onkeyup="adjust_textarea(this)"></textarea>
        <input type="submit" value="Add" />
      </form>

express中的错误处理程序必须具有4个参数,而不是3个

因此,您的/add路线:

router.post('/add', function (err, req, res) {

实际上是一条普通路由,第一个参数使用req (请求)填充,第二个参数使用res (响应)填充,第三个参数使用next函数填充。 因为您将第二个参数命名为req ,所以创建了一个令人困惑的场景,其中req是响应, err是请求, res是下一个函数。

因为响应没有body属性,并且您已将响应命名为req ,所以您的req.body.title试图将属性req.body.title为未定义的值( body )。 重命名您的参数req, res, next ,我想您会更好。

在get方法中,您将标题引用为event.title但在帖子中将其引用为title 为什么?

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM