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)
}
};
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?).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With