0

I'm performing CRUD operations for RESTFUL API using MySQL and NodeJS express. There's an error in inserting a record into DB, I don't know why I was getting a problem as I have copied the insert query from SQL where it was working properly. I wrote a query like this var sql = 'INSERT INTO tblltest values(null, "+req.body.Name+")';. I've tested it using PostMan instead of adding name it adds +req.body.Name+ param into DB. Please help me out to solve this issue.

Here are my lines of code:

var express = require('express');
var mysql = require('mysql');
var app = express();
const bodyparser = require('body-parser');

var connection = mysql.createConnection({
    
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'sampledb'
    
});
app.post('/' , function(req , resp) {
    var bg = req.body;
    
    var sql = 'INSERT INTO `tblltest` values(null, "+req.body.Name+")';

    connection.query(sql , function(error , rows , fields){
        if(!error){
            
            console.log('Successful added!! \n');
            resp.json(rows);
            
        }else{
            console.log('Error in adding');
        }
        
    });
})

app.listen(1337);
rohitt
  • 1,124
  • 6
  • 18
  • Your query string is wrong. Please use `'INSERT INTO "tblltest" values(null, "'+req.body.Name+'")';` – nkkumawat Dec 26 '20 at 05:29
  • @NarendraKumawat Sir, it's not working. "TypeError: Cannot read property 'Name' of undefined " – Usama Hafeez Official Dec 26 '20 at 06:11
  • This has too many issue in my opinion. Please [edit your question](https://stackoverflow.com/review/suggested-edits/27933646) and include the html part of the code. You usually got `undefined` value if the the `req.body.something` didn't get the value from the field where your html have the action to post. So, firstly I want to see how do you get `req.body.Name` from the html counterpart. Also what does `var bg = req.body;` do? – FanoFN Dec 26 '20 at 07:18

1 Answers1

0

I think you are missing single quotes in your sql variable, instead of

var sql = 'INSERT INTO `tblltest` values(null, "+req.body.Name+")';

Replace with:

var sql = 'INSERT INTO `tblltest` values(null, "' + req.body.Name + '")';

Edit: By the looks of your app configuration, looks like bodyParser its not being used by your app, try adding this to your app (before app.post(...)) so it would be able to use body parser to get info from POST and/or URL parameters:

app.use(bodyparser.urlencoded({ extended: true }));
app.use(bodyparser.json()); 

Also, use console.log to see if there is any data in your body object, try adding it before your sql query:

console.log(req.body);
Alejandro
  • 325
  • 4
  • 10
  • Sir, it's not working. "TypeError: Cannot read property 'Name' of undefined " – Usama Hafeez Official Dec 26 '20 at 06:12
  • @UsamaHafeez Please check my edited answer. – Alejandro Dec 26 '20 at 06:31
  • Now I code as you have asked but still getting an error in postman. The problem there is if the name is passed in the URL (http://localhost:1337/Usama) it gives the error Cannot POST/Usama. but if URL(http://localhost:1337) like this saves empty string in DB. `app.use(bodyparser.urlencoded({ extended: true })); app.use(bodyparser.json()); app.post('/' , function(req , resp) { console.log(req.body); `var sql = 'INSERT INTO `tblltest` values(null, "' + req.params.Name + '" )';` connection.query( sql , function(error , rows , fields){ })` – Usama Hafeez Official Dec 26 '20 at 06:56
  • That's wierd, if you are sending the post like this https://imgur.com/B9zuLaS, req.body should be having the name parameter. I'm running out of ideas, if you are using express v4.16.0 or greater.. you could give a try to this answer https://stackoverflow.com/questions/4295782/how-to-process-post-data-in-node-js sorry I wanst able to help. – Alejandro Dec 26 '20 at 07:36