无法在我的程序中运行查询

6fe3ivhb  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(221)

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

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\'' }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题