0

I have a problem when inserting a row into my database, it adds the row but after that sends this parse error. From frontend I am sending an JSON POST request, and in backend I've installed mysql from npm and used it to connect to database.

The problem is with first\ in query how can I disable that or anything else that I can do? It works if I remove them from query.
I've tried with INSERT INTO VALUES and it doesn't works either.

mysql debug:

  0|index    | { name: 'rette',
  0|index    |   groupMuscles: 'Back',
  0|index    |   datum: '2017-10-29',
  0|index    |   reps1: '2',
  0|index    |   kgs1: '2',
  0|index    |   reps2: '2',
  0|index    |   kgs2: '2',
  0|index    |   reps3: '2',
  0|index    |   kgs3: '2' }
  0|index    | --> ComQueryPacket
  0|index    | ComQueryPacket {
  0|index    |   command: 3,
  0|index    |   sql: 'INSERT INTO `EXERCISE` SET `name` = \'rette\', `groupMuscles` = \'Back\', `datum` = \'2017-10-29\', `reps1` = \'2\', `kgs1` = \'2\', `reps2` = \'2\', `kgs2` = \'2\', `reps3` = \'2\', `kgs3` = \'2\'' }
  0|index    | <-- OkPacket
  0|index    | OkPacket {
  0|index    |   fieldCount: 0,
  0|index    |   affectedRows: 1,
  0|index    |   insertId: 25,
  0|index    |   serverStatus: 2,
  0|index    |   warningCount: 0,
  0|index    |   message: '',
  0|index    |   protocol41: true,
  0|index    |   changedRows: 0 }
  0|index    | {}
  0|index    | --> ComQueryPacket
  0|index    | ComQueryPacket { command: 3, sql: 'INSERT INTO `EXERCISE` SET ' }
  0|index    | <-- ErrorPacket
  0|index    | ErrorPacket {
  0|index    |   fieldCount: 255,
  0|index    |   errno: 1064,
  0|index    |   sqlStateMarker: '#',
  0|index    |   sqlState: '42000',
  0|index    |   message: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'\' at line 1' }
  0|index    | Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
  0|index    |     at Query.Sequence._packetToError (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
  0|index    |     at Query.ErrorPacket (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
  0|index    |     at Protocol._parsePacket (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/Protocol.js:279:23)
  0|index    |     at Parser.write (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/Parser.js:76:12)
  0|index    |     at Protocol.write (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/Protocol.js:39:16)
  0|index    |     at Socket.<anonymous> (/home/brad/node-js-sample/node_modules/mysql/lib/Connection.js:103:28)
  0|index    |     at emitOne (events.js:96:13)
  0|index    |     at Socket.emit (events.js:188:7)
  0|index    |     at readableAddChunk (_stream_readable.js:176:18)
  0|index    |     at Socket.Readable.push (_stream_readable.js:134:10)
  0|index    |     --------------------
  0|index    |     at Protocol._enqueue (/home/brad/node-js-sample/node_modules/mysql/lib/protocol/Protocol.js:145:48)
  0|index    |     at Connection.query (/home/brad/node-js-sample/node_modules/mysql/lib/Connection.js:208:25)
  0|index    |     at app.post (/home/brad/node-js-sample/index.js:44:6)
  0|index    |     at Layer.handle [as handle_request] (/home/brad/node-js-sample/node_modules/express/lib/router/layer.js:95:5)
  0|index    |     at next (/home/brad/node-js-sample/node_modules/express/lib/router/route.js:137:13)
  0|index    |     at Route.dispatch (/home/brad/node-js-sample/node_modules/express/lib/router/route.js:112:3)
  0|index    |     at Layer.handle [as handle_request] (/home/brad/node-js-sample/node_modules/express/lib/router/layer.js:95:5)
  0|index    |     at /home/brad/node-js-sample/node_modules/express/lib/router/index.js:281:22
  0|index    |     at Function.process_params (/home/brad/node-js-sample/node_modules/express/lib/router/index.js:335:12)
  0|index    |     at next (/home/brad/node-js-sample/node_modules/express/lib/router/index.js:275:10)

index.js

  app.post('/add',(request,response)=>{
    let pushToSrv = request.body;

    //insert new JSON

    // `name` = \'name\' not accepted .)

    let sql = "INSERT INTO `EXERCISE` SET ?";


  db.query(sql,pushToSrv,(err2,resPush) => {
      if(err2)
        throw err2;
        else {

    response.status(200).end('added');
            }
      })//end_insert

  });
zivce
  • 444
  • 5
  • 10

1 Answers1

0

Before executing query you need check filling pushToSrv.

How to test

Artem Ilchenko
  • 1,050
  • 9
  • 20