MelgoV MelgoV - 2 months ago 16
Node.js Question

How dynamically assigning values ​to a property of an object?

I am coding a node module with

mssql
for node.js, in this module I have a function called
sp_recordset
which allows to execute a stored procedure by just sending a db connection, the name of the stored procedure, the parameters, I added a callback function to return my recordset, in the parameters I send an array with the names of the parameters, the values and the SQL Server data types, this is the Array:

[{name:'num',type:'Int',value:2},
{name:'num1',type:'Int',value:1}]


This is my function:

exports.sp_recordset = function(conex, storedProcedure, parameters, cb) {

var connection = new mssql.Connection(conex, function(err) {
if(err){
console.log(err);
}

var request = new mssql.Request(connection);

for(index=0;index<parameters.length;++index)
{
request.input(parameters.name, mssql.Int, parameters.value);
}

request.execute(storedProcedure, function(err, recordsets, returnValue) {
if(err){
console.log(err);
}
cb(recordsets);
});

});

};


In this line line is my problem:

request.input(parameters.name, mssql.Int, parameters.value);


As I said, the need is a dynamic function, the second parameter (SQL Server data type also has to by dynamic), which is the best way to assign the data type to
mssql
? I have tried this but it doesn't works:

request.input(parameters.name, mssql.(parameters.type), parameters.value);

Answer

It looks like you're trying to access variable property by its name, not to assign it.

If so, the following should work for you:

request.input(parameters.name, mssql[parameters.type], parameters.value);