0

I am having trouble with inserting data into my database on Heroku (MySQL, ClearDB). Whenever I am trying to execute INSERT query it just does not work, nothing is inserted, no error messages at all. SELECT query works fine and I confirmed that but no idea what am I doing wrong in this case. Please find a snippet below: So this is my INSERT function:

getUserDetails(value, (err, result)=>{
        if(err){
          return;
        }
        if(result.length > 0){
          socket.emit("verify", {type: true, message: "Username taken"});
        }else{
          const connSocket = getDbConnectionSocket();
          connSocket.connect(err=>{
            if (err){
              return;
            }
            const sql="INSERT INTO tableName (column1, column2, column3) VALUES ?";
            const values = [value.key1,value.key2, "value2AsString"];
            connSocket.query(sql, [values], (err, result)=>{
              if(err){
                return;
              }
            });
            connSocket.end();
          });
        }
      })

and this is my getUserDetails function:

function getUserDetails(value, callback){
  const connSocket = getDbConnectionSocket();
  connSocket.connect(err=>{
    if (err){
      return;
    }
    connSocket.query(`SELECT * FROM table WHERE column="${value.key1}"`, callback);
    connSocket.end();
  });
}

DB Config is 100% correct, SELECT requests are working, socket.io is working perfectly fine. I am sending this request from the index.js file which is now hosted on my localhost and is an http server. This in the future will be as well hosted on heroku.

Can anyone explain me what is going on here and how to fix it? I would be grateful for correct explanation on the problem.

Łukasz
  • 13
  • 5
  • You have several silent returns in your INSERT function. Have you checked that the ‘connSocket.query’ code is actually being called? – terrymorse Apr 19 '20 at 14:46
  • @terrymorse i can't believe I have missed it. Your response helped me to solve the issue. basically it was syntax error in the query itself: const sql="INSERT INTO tableName (column1, column2, column3) VALUES ?" changed to const sql="INSERT INTO tableName (column1, column2, column3) VALUES (?)" adding () over ?. Thanks a lot! – Łukasz Apr 19 '20 at 14:57
  • Writing raw SQL is error-prone. Consider using an ORM like Sequelize, it should reduce the errors. https://github.com/sequelize/sequelize – terrymorse Apr 19 '20 at 15:10

1 Answers1

0

The issue was syntax error in the query to DB. I have changed

INSERT INTO tableName (column1, column2, column3) VALUES ?

to

INSERT INTO tableName (column1, column2, column3) VALUES (?)

adding () over ? and it now works all fine!

Łukasz
  • 13
  • 5