Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

sql - 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)
    }

};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...