title: post to SQL Server api with Express.js
body: <p>I´ve been testing Apis with Express.js from SQL Server database. With get i have no problems, but i cannot post.</p>
  
  <p>This is y code so far:</p>
  
  <pre><code>    const express = require('express');
  const bodyParser = require('body-parser');
  const sql = require('mssql');
  const app = express();
  
  app.use(bodyParser.json());
  
  app.use(function (req, res, next) {
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
      res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
      next();
  });
  
  const server = app.listen(process.env.PORT || 8080, function () {
      var port = server.address().port;
      console.log("App now running on port", port);
  });
  
  const dbConfig = {
      user: "daUser",
      password: "daPass",
      server: "daServer",
      database: "DaDB"
  }
  
  const executeQuery = function (res, query) {
      sql.connect(dbConfig, function (err) {
          if (err) {
              console.log(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(err);
                      res.send(err);
                  }
                  else {
                      res.send(result);
                  }
              });
          }
      });
  }
  
  app.get("/api/HolidayBaseApi", function (req, res) {
      var query = "SELECT * FROM [HolidaysBase]";
      executeQuery(res, query);
  })
  
  app.post("/api/HolidayBaseApi", function (req, res) {
      var query = "INSERT INTO [HolidaysBase] (EmployeeNumber, PeriodBegin, PeriodEnd, WorkedYears, DaysPerYear, TakenDays, RemainingDays) VALUES (req.body.EmployeeNumber, req.body.PeriodBegin, req.body.PeriodEnd, req.body.WorkedYears, req.body.DaysPerYear, req.body.TakenDays, req.body.RemainingDays)";
      executeQuery(res, query);
  })
  </code></pre>
  
  <p>When i'm triying to make the post, i get an error which says:</p>
  
  <pre><code>The multi-part identifier \"req.body.RemainingDays\" could not be bound.
  </code></pre>
  
  <p>The error list all my fields with the same error. The format of every field it's fine. I'm sending the data like this in postman:</p>
  
  <pre><code>{
      "EmployeeNumber": "9",
      "PeriodBegin": "2018-10-15",
      "PeriodEnd": "2019-10-15",
      "WorkedYears": "6",
      "DaysPerYear": "18",
      "TakenDays": "0",
      "RemainingDays": "18"
  }
  </code></pre>
  
  <p>Someone knows why is not letting me post the info?</p>
  
  <p>I'm using Javascript, Express.js, Node.</p>
  
  <p><strong>--- EDITION ---</strong></p>
  
  <p>I've found two solutions. The first solution next:</p>
  
  <pre><code>    app.post("/api/HolidayBaseApi", function (req, res) {        
      var query = "INSERT INTO [HolidaysBase] (EmployeeNumber, PeriodBegin, PeriodEnd, WorkedYears, DaysPerYear, TakenDays, RemainingDays) VALUES ('"+req.body.EmployeeNumber+"','"+req.body.PeriodBegin+"','"+req.body.PeriodEnd+"','"+req.body.WorkedYears+"','"+req.body.DaysPerYear+"','"+req.body.TakenDays+"','"+req.body.RemainingDays+"')";
      executeQuery(res, query);
  });
  </code></pre>
  
  <p>With my code like this, i could make the post. But there was some comments that this way it's a little dangerous, 'cause it's easier to get an SQL attack injection. And found another solution: On the <code>const executeQuery</code> need to add <code>parameters</code> like this:</p>
  
  <pre><code>const executeQuery = function (res, query, parameters) {
      sql.connect(dbConfig, function (err) {
          if (err) {
              console.log(err);
              res.send(err);
          }
          else {
              // create Request object
              var request = new sql.Request();
              // query to the database
  
              parameters.forEach(function (p) {
                  request.input(p.name, p.sqltype, p.value);
              });
  
              request.query(query, function (err, result) {
                  if (err) {
                      console.log("Error al correr query en la base: " + err);
                      res.send(err);
                  }
                  else {
                      res.send(result);
                      sql.close();
                  }
              });
          }
      });
  }
  </code></pre>
  
  <p>Then, on the <code>app.post</code>:</p>
  
  <pre><code>app.post("/api/HolidayBaseApi", function (req, res) {
      var parameters = [
          { name: 'EmployeeNumber', sqltype: sql.Int, value: req.body.EmployeeNumber },
          { name: 'PeriodBegin', sqltype: sql.VarChar, value: req.body.PeriodBegin },
          { name: 'PeriodEnd', sqltype: sql.VarChar, value: req.body.PeriodEnd },
          { name: 'WorkedYears', sqltype: sql.Int, value: req.body.WorkedYears },
          { name: 'DaysPerYear', sqltype: sql.Int, value: req.body.DaysPerYear },
          { name: 'TakenDays', sqltype: sql.Int, value: req.body.TakenDays },
          { name: 'RemainingDays', sqltype: sql.Int, value: req.body.RemainingDays },
      ];
      var query = "INSERT INTO [HolidaysBase] (EmployeeNumber, PeriodBegin, PeriodEnd, WorkedYears, DaysPerYear, TakenDays, RemainingDays) VALUES (@EmployeeNumber, @PeriodBegin, @PeriodEnd, @WorkedYears, @DaysPerYear, @TakenDays, @RemainingDays)";
      executeQuery(res, query, parameters);
  });
  </code></pre>
  
  <p>It works this way too </p>