Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a stored procedure with NodeJS and MSSQL package error

Im trying to get the MSSQL nodejs package to return the results of a stored procedure from Microsoft SQL server using the code below. However the error I get is...

[TypeError: Cannot read property 'type' of undefined]

I'm not sure I have done the inputs correctly as I couldn't find an example with more than one input anywhere online.

Any ideas?

exports.executeSqlStoredProd = function (callback) {
    var conn = new sqlDb.Connection(settings.dbConfig)
    conn.connect().then(function () { 
        var req = new sqlDb.Request(conn);
        req.input('ProductEntryID', req.Int, 3299);
        req.input('LoginEntryID', req.Int, 4);
        req.input('TempLoginEntryId', req.Int, -1);
        req.input('AddToWishList', req.Bit, 0);
        req.input('WebPortalId', req.Int, 0);
        req.execute('J_ViewAProduct').then(function(err, recordsets) {
        console.log(recordsets);
        callback(recordsets)
    })}).catch(function(err){ 
        console.log(err);
        callback(null, err);
    });
}

I did the request sucessfully using the package "Seriate" but would prefer to use mssql. The code that worked for "Seriate" is below.

exports.getVAP = function(req, resp, pid) {  
    sql.execute({
        procedure: "J_ViewAProduct",
        params: {
            ProductEntryID: {
                type: sql.INT,
                val: pid
            },
            LoginEntryID: {
                type: sql.Int,
                val: 4
            },
            TempLoginEntryId: {
                type: sql.Int,
                val: -1
            },
            AddToWishList: {
                type: sql.Bit,
                val: 0
            },
            WebPortalId: {
                type: sql.Int,
                val: 0
            }
        }
    }).then(function(results){
        console.log(results)
        httpMsgs.sendJSON(req, resp, results)
        //httpMsgs.sendJSON(req, resp, results[0])
        resp.end();
    }), function(err){
        httpMsgs.show500(req, resp, err)
    }

};
like image 375
Adam91Holt Avatar asked Aug 12 '16 04:08

Adam91Holt


People also ask

What is wrong with stored procedures?

Stored procedures introduce a cliff (or disconnect) between coherent functionality, because the domain logic gets split between the application- and the database layer. It's rarely clear where the line is drawn (e.g. which part of a query should go into the application layer and which part into the database layer?).


2 Answers

I think the line's

req.input('ProductEntryID', req.Int, 3299);
req.input('LoginEntryID', req.Int, 4);
req.input('TempLoginEntryId', req.Int, -1);
req.input('AddToWishList', req.Bit, 0);
req.input('WebPortalId', req.Int, 0);

which has req.input thats wrong it seems.

Please try this code

var sql = require('mssql');

var config = {
    user: 'sa',
    password: '---',
    server: 'localhost', // You can use 'localhost\\instance' to connect to named instance
    database: 'Test'
}

var getCities = function() {
  var conn = new sql.Connection(config);
  conn.connect().then(function(conn) {
    var request = new sql.Request(conn);
    request.input('City', sql.VarChar(30), 'Cbe');
    request.input('NameNew', sql.VarChar(30), 'Cbe');
    request.execute('spTest').then(function(err, recordsets, returnValue, affected) {
      console.dir(recordsets);
      console.dir(err);
    }).catch(function(err) {
      console.log(err);
    });
  });
}

getCities();

I tried this myself and its giving the results.

like image 131
shanmugharaj Avatar answered Sep 23 '22 17:09

shanmugharaj


I don't know if this would be helpful or not but this is how I did

let executeQuery = async (value, country) => {
    try {
        let pool = await sql.connect(dbConfig);
        let results = await pool.request()
            .input('input_parameter', sql.Int, value)
            .input('Country', sql.VarChar(50), country)
            // .output('output_parameter', sql.VarChar(50))
            .execute('procedure_name')

        console.dir(results);

    } catch (err) {
        res.json({
            "error": true,
            "message": "Error executing query"
        })
    }
}

executeQuery(value, country);

I've used async and await method to make it more readable.

like image 34
buggy_coder Avatar answered Sep 20 '22 17:09

buggy_coder