1

After fill information in below WebForm and submit my data ,I got undefined values under ms sql server records.

//my html file

<!doctype html>
<html lang="en">
  <head>
    <title>Employees</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

   
  </head>
  <body>
    <h1><center>Add a new Employee</center></h1><br>

    <center>
      <form action = "http://localhost:8080/api" method = "POST" enctype = "multipart/form-data">
         <input type ="text" name ="first_Name" placeholder="Enter First Name"><br>
         <br>
         <input type ="text" name = "last_name" placeholder="Enter Last Name"><br>
        
         <input type = "submit" value = "Submit">
      </form><br>
    </center>

    
    <!-- Optional JavaScript -->
      <script src="Node.js"></script>
   
  </body>
</html>

This is my Node.js code :

//Initiallising node modules
var express = require('express');
var bodyPasrser = require('body-parser');
var sql = require('mssql');
var app = express();


//Body Parser to parse to JSON
app.use(bodyPasrser.json());


//CORS Middleware
app.use(function(req,res,next){
    res.header("Access-Control-Allow-Origin","*");
    res.header("Access-Control-Allow-Methods","GET,HEAD,POST,PUT,OPTIONS");
    res.header("Access-Control-Allow-Headers","Origin,X-Requested-With,contentType,Content-Type,Accept,Authorization");
    next();
});


//Setting up server
var server = app.listen(process.env.PORT||8080,function(){
    var port = server.address().port;
    console.log("App now running on port ",port);
});


//setup database connection



var dbconfig = {
    user:"sa",
    password:"--------",
    server : "localhost",
    database: "Test"  
};


// ConnectionPool


//connect to the database 
var executeQuery = function(res,query){
    sql.connect(dbconfig,function(err){
        if(err){
            console.log("there is a database connection error -> "+err);
            res.send(err);
        }
        else{
            // create request object
            var request = new sql.Request();
            // query to the database
            request.query(query,function(err,result){
                if(err){
                    console.log("error while querying database -> "+err);
                    res.send(err);
                }
                else{
                    res.send(result);
                    sql.close();
                }
            });
        }
    });
}


// Change execute query to accept parameters.
var executeQuery = function(res,query,parameters){
    sql.connect(dbconfig,function(err){
        if(err){
            console.log("there is a database connection error -> "+err);
            res.send(err);
        }
        else{
            // create request object
            var request = new sql.Request();

            // Add parameters
            parameters.forEach(function(p) {
                request.input(p.name, p.sqltype, p.value);
            });

            // query to the database
            request.query(query,function(err,result){
                if(err){
                    console.log("error while querying database -> "+err);
                    res.send(err);
                }
                else{
                    res.send(result);
                    sql.close();
                }
            });
        }
    });
}



//POST API
app.post("/api", function(req , res){

    var parameters = [
      { name: 'First_Name', sqltype: sql.NVarChar, value: req.body.First_Name},
      { name: 'Last_name', sqltype: sql.NVarChar,  value: req.body.Last_name},
    ];

        var query = "INSERT INTO test.dbo.name (First_Name,Last_name) VALUES ('" +req.body.First_Name+"','"+req.body.Last_name+"')";
    executeQuery (res, query, parameters);
});

After display the records under SQL server I found that data I submitted , as "undefined" and it is not my data I submitted in the webform , please what is wrong in my code.

Please can give simple example in how to solve such case

thank you very much

1 Answer 1

1

There is typo in form data you are using,

var query = "INSERT INTO test.dbo.name (First_Name,Last_name) VALUES ('" +req.body.First_Name+"','"+req.body.Last_name+"')";

should be,

var query = "INSERT INTO test.dbo.name (First_Name,Last_name) VALUES ('" +req.body.first_Name +"','"+req.body.last_name+"')";
Sign up to request clarification or add additional context in comments.

2 Comments

I done the changes based on your shared comments, but still the issue is the same .
what do you see in logs when post api is called, check out the req param in log. app.post("/api", function(req , res){

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.