Home > OS >  I am new to node js and I have some problem with post using postman
I am new to node js and I have some problem with post using postman

Time:12-24

I am new to node.js and learning it for quite few days and now I'm stuck with this app.post which is not working for me. If possible please let me know how to do app.update so it might be a big help in my learning process. Kindly waiting for reply.

const mysql = require('mysql');
const express = require('express');
var app = express();
const bodyparser = require('body-parser');
app.use(bodyparser.urlencoded({extended: false}));
app.use(bodyparser.json());
app.listen(8000);
var mysqlconnection = mysql.createConnection(
    {
        host: 'localhost',
        user: 'Naveen',
        password: '',
        database: 'employeedb',
        multipleStatements: true
    }
);

mysqlconnection.connect((err)=>{
    if(!err)
    console.log("DB connection successfull");
    else
    console.log('DB connection failed \n Error : '  JSON.stringify(err, undefined, 2) );
});
app.post('/employee' ,function (req, res, next) {
    Name = req.query.Name, 
    Empcode = req.query.Empcode,
    Salary = req.query.Salary 
    let sql = "INSERT INTO employee (Name, Empcode, Salary) VALUES (? , ?, ?)";  
    mysqlconnection.query('sql, (Name, Empcode, Salary) ', (err, rows, fields) => {
        if (!err)
            res.send("Insert succeed");
        else
            console.log(err);
        });
    });    
    ```
and then I get these error messages:

**PS C:\xampp\htdocs\first app> node index.js DB connection successfull Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'sql, (Name, Empcode, Salary)' at line 1
   (C:\xampp\htdocs\first app\node_modules\express\lib\router\index.js:275:10) {   code: 'ER_PARSE_ERROR',   errno: 1064,   sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'sql, (Name, Empcode, Salary)' at line 1",   sqlState: '42000',   index: 0,   sql: 'sql, (Name, Empcode, Salary) ' }**

CodePudding user response:

You have typo in your query.

// WRONG
mysqlconnection.query('sql, (Name, Empcode, Salary) ', (err, rows, fields)...

It should not be inside quotes, and variables should be an array.

// CORRECT
mysqlconnection.query(sql, [Name, Empcode, Salary], (err, rows, fields)...

CodePudding user response:

The error is clear. It is saying "You have an error in your SQL syntax". And you should check your syntax at "near 'sql, (Name, Empcode, Salary)'". This line here

mysqlconnection.query('sql, (Name, Empcode, Salary) ',

you are passing the string literal "sql, (Name, Empcode, Salary) ", while you meant to pass in the variable sql that you created in the line above.

  • Related