1

I'm trying to call a register stored procedure from SQL Server. I'm using NodeJS with the mssql package. I got most of the code from the mssql Npm documentation. But it still doesn't work.

The code basically tries to initiate a ConnectionPool as a read the last update renamed connection to ConnectionPool, create a new request, introduce the parameters and the execute the stored procedure. I have it this way because I need to change the connection variable to another database every once in a while.

The error I get is:

ConnectionError: Connection is closed.

My code:

const sql = require('mssql');

const config = require('../config/dbPoly');

module.exports.registerUser = function(pUserName, pPassword, pNombre, pApellidos, pFK_TipoUsuario, callback) {

  var connection = new sql.ConnectionPool(config);
  var request = new sql.Request(connection);
  request.input('input_parameter', sql.VARCHAR(25), pUserName);
  request.input('input_parameter', sql.VARCHAR(16), pPassword);
  request.input('input_parameter', sql.VARCHAR(25), pNombre);
  request.input('input_parameter', sql.VARCHAR(50), pApellidos);
  request.input('input_parameter', sql.Int, pFK_TipoUsuario);

  request.execute('RegistrarUsuario', (err, result)=>{
    if(err){
      console.log(err);
    }
    else{
      console.log(result);
    }
  });
};

2 Answers 2

2

There is this async version of implementation. Check if this serves your purpose

const sql = require('mssql');

const config = require('../config/dbPoly');

module.exports.registerUser = async function(pUserName, pPassword, pNombre, pApellidos, pFK_TipoUsuario) {

    try {

        let connection = await sql.connect(config)

        let result = await connection.request()
            .input('input_parameter', sql.VARCHAR(25), pUserName);
            .input('input_parameter', sql.VARCHAR(16), pPassword);
            .input('input_parameter', sql.VARCHAR(25), pNombre);
            .input('input_parameter', sql.VARCHAR(50), pApellidos);
            .input('input_parameter', sql.Int, pFK_TipoUsuario);
            .execute('RegistrarUsuario');

        return result;
    } catch (err) {
        console.log(err); // ... error checks 
    }

};

sql.on('error', err => {
    // ... error handler 
})
Sign up to request clarification or add additional context in comments.

10 Comments

Remove the port config, as it is default 1443. Don't use if using named instance
So your problem solved ? Upvote if found helpful, you are welcome!
Actually as that code is, node send the following error: Request error :RequestError: Procedure or function 'RegistrarUsuario' expects parameter '@pNombreUsuario', which was not supplied. Any ideas.
What is exact the stored procedure syntax of mssql you are calling ? You might be missing input or output parameter there to pass from node.
The parameters are supposed to be added to the request by calling request.input() like I did in the corrected code in this thread. But it still gives me the same error. stackoverflow.com/questions/44930584/…
|
0

If someone else faces this problem, Prabodh's answer works perfectly.

I then faced another problem, it said the localhost was undefined in 1500ms. The reason was because my server field in the json config file had the following format: 'localhost\Server-Name'. I changed it just 'localhost' and problem fixed.

Also make sure your server has SQL Authentication enabled and not just Windows Authentication.

1 Comment

Also if you are using named instance, avoid adding port in config as per the documentation of the module. npmjs.com/package/mssql#general-same-for-all-drivers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.