0

It would be great if anyone can help me out. I am new to node.js, and I am trying to send the data into MSSQL database for my initial project, and as per project requirement I was not able to use any other DB other than MSSQL, and I am getting error value into DB while I execute the insert query. I tried to figure out the error more than a day but end up with nothing. Could anyone help me to fix this error.?

Thanks in advance.

   // Server.js


var express = require('express');
var app = express();
var port = process.env.port || 3000;

var bodyParser = require('body-parser');
// create application/x-www-form-urlencoded parser
app.use(bodyParser.urlencoded({ extended: true }));
// create application/json parser
app.use(bodyParser.json());

var ProductController = require('./Controller/ProductController')();


app.use("/app/Data", ProductController)

app.listen(port, function () {
    var datetime = new Date();
    var message = "Server runnning on Port:- " + port + "Started at :- " + datetime;
    console.log(message);
});



  //  App.js


var express = require('express');
var router = express.Router();
var sql = require("mssql");
var conn = require("../connection/connect")();

var routes = function () {
    router.route('/')
        .get(function (req, res) {
            conn.connect().then(function () {
                var sqlQuery = "SELECT * FROM ArduinoSensor";
                var req = new sql.Request(conn);
                req.query(sqlQuery).then(function (recordset) {
                    res.json(recordset.recordset);
                    conn.close();
                })
                    .catch(function (err) {
                        conn.close();
                        res.status(400).send("Error while Receive Data");
                    });
            })
                .catch(function (err) {
                    conn.close();
                    res.status(400).send("Error");
                });
        });
    router.route('/')
        .post(function (req, res) {
            conn.connect().then(function () {
                var transaction = new sql.Transaction(conn);
                transaction.begin().then(function () {
                    var request = new sql.Request(transaction);
                    request.input("start_time", sql.VarChar(50), req.body.start_time)
                    request.input("end_time", sql.VarChar(50), req.body.end_time)
                    request.input("length_time", sql.VarChar(50), req.body.length_time)
                    request.execute("Usp_InsertSensor").then(function () {
                        transaction.commit().then(function (recordSet) {
                            conn.close();
                            res.status(200).send(req.body);
                        }).catch(function (err) {
                            conn.close();
                            res.status(400).send("Error while inserting data");
                        });
                    }).catch(function (err) {
                        conn.close();
                        res.status(400).send("Error while inserting data");
                    });
                }).catch(function (err) {
                    conn.close();
                    res.status(400).send("Error while inserting data");
                });
            }).catch(function (err) {
                conn.close();
                res.status(400).send("Error while inserting data");
            });
        });

    return router;
};
module.exports = routes;

PS:- I have attached the outcome of the coding Outcome of the image

  • Can you include the error message that you get ? – Marc Guillot Oct 03 '18 at 08:00
  • You are using varchar data types for time data. Maybe there occurs an error with this data conversion. You can execute SQL Profiler and see what exactly the SQL Engine is trying to execute – Eralper Oct 03 '18 at 08:16
  • @Eralper, Thanks for your reply, i have changed all the data type, but still it's not working..! – Jathu Jathus Oct 03 '18 at 08:35
  • @MarcGuillot,Thanks for your reply. I did not get any Error while executing the code. null value has been inserted into my mssql DB once i execute above coding – Jathu Jathus Oct 03 '18 at 09:04
  • Is this the stored procedure Usp_InsertSensor? Place a code block to check if input parameters are NULL or not, you can raise an error message or log into a temporarily created table – Eralper Oct 03 '18 at 09:28
  • @Eralper, Thank you for the reply, i have followed all those process, but still end up with same NULL value return into DB. and i did not see any error messages while executing the coding. thanks again – Jathu Jathus Oct 04 '18 at 02:10
  • 1
    I Found the solutions from here.. [Solutions](https://stackoverflow.com/questions/38909920/running-a-stored-procedure-with-nodejs-and-mssql-package-error?rq=1) – Jathu Jathus Oct 04 '18 at 03:32

0 Answers0