首页 > 解决方案 > 无法在我的程序中运行查询

问题描述

当我发布请求时 mysql 查询出错,但是当我使用具有相同查询的 mysql 工作台插入数据时工作正常 当我发布请求时 mysql 查询出错,但是当我使用具有相同查询的 mysql 工作台插入数据时工作正常

Router.post('/page/post/add', (req, res) => {
    let body = _.pick(req.body, ['title', 'content']);
    console.log(body);
    body["featured"] = 'faketext';
    body["slug"] = slug(body.title);
    body["created_by"] = 'faketext';
    let sql = "INSERT INTO bwa_post(category_id,title, content, featured, slug, created_by) SELECT id, ? FROM bwa_category WHERE category_name = ?"
    let query = db.query(sql, [body, req.body.category_name], (err, result) => {
        if(err) {
            console.log(err);
            req.flash('error_msg', '500 : Database Error...');
            res.status(500).end();
        } 
        req.flash('success_msg', 'Post Added Successfully');
        res.status(200).end();
    });
});

错误->

{ title: 'dcdcsd',
  content: '<p>Here You Can Add Content</p>\r\n' }
{ Error: ER_BAD_FIELD_ERROR: Unknown column 'title' in 'field list'
    at Query.Sequence._packetToError (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
    at Query.ErrorPacket (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
    at Protocol._parsePacket (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/Connection.js:103:28)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    --------------------
    at Protocol._enqueue (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Connection.query (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/Connection.js:208:25)
    at Router.post (/media/gaurav/301286221285ED62/NEW DESIGN/routes/adminRoute.js:325:20)
    at Layer.handle [as handle_request] (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/layer.js:95:5)
    at next (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/layer.js:95:5)
    at /media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/index.js:335:12)
    at next (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/index.js:275:10)
  code: 'ER_BAD_FIELD_ERROR',
  errno: 1054,
  sqlMessage: 'Unknown column \'title\' in \'field list\'',
  sqlState: '42S22',
  index: 0,
  sql: 'INSERT INTO bwa_post(category_id,title, content, featured, slug, created_by) SELECT id, `title` = \'dcdcsd\',`content` = \'<p>Here You Can Add Content</p>\\r\\n\', `featured` = \'dscsdcsd\', `slug` = \'dcdcsd\', `created_by` = \'Gaurav\' FROM bwa_category WHERE category_name = \'fakedata\'' }

标签: mysqlnode.jsexpress

解决方案


推荐阅读