1

Before I have made a test run and Everything was ok, but know when I am trying to insert a new static data it is saying that result is undefined. How to solve this? error from terminal

const mysql = require("mysql")
const con = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"",
    database:"magebitdb"
});

con.connect((err)=>{
    if(err) throw err;
    console.log("connected to DB");
    

    // const sql =`INSERT INTO subscribers(email) VALUES (${email})`;
    const sqlInsert = ("INSERT INTO subscribers(email) VALUES ?")
    const values=[
        "test value"
    ]
    con.query(sqlInsert,[values],(err,result)=>{
        console.log(`inserted data is: ${result}`);
        console.log(values);
    });
});

Riad8242
  • 23
  • 5

1 Answers1

0

I have found the solution for this error.

I have console logged err in con.query and I noticed that in error there was no brackets and it was causing an error in the SQL syntax.

Basically I added brackets to sqlInsert and it worked const sqlInsert = ("INSERT INTO subscribers(email) VALUES (?)")

Riad8242
  • 23
  • 5