Vladimir Zaguzin
Vladimir Zaguzin

Reputation: 303

node-mssql cannot read property length of undefined from dbconfig user name

hello i'm looking for some help with this problem i'm working on rest api for my db got this problem

var bufferLength = 64 + domain.length * 2 + username.length * 2 + lm v2len + ntlmv2len + 8 + 8 + 8 + 4 + server_data.length + 4;

TypeError: Cannot read property 'length' of undefined

I have tried several ways to solve this problem, but nothing really helped. solutions that posted online wouldn't work on my machine at all or return same error for some reason

there is my code

const sql = require('mssql');
const express = require('express')
const app = express()


var config = {
  userName: 'user' , 
  password: 'pass', 
  domain: "AD",
  server: serversIP,
  database: 'test',
  port: 2222,
  debug: true,
  driver: 'tedious',
  options: {
    database:"test",
    instanceName : "instance"
  } 
}

app.get("/getUsers" , (req, res)=>{
  sql.connect(config, function (err) {
    var request = new sql.Request();
    if (err) {
        console.log(err);
        return;
    }
      req.query("SELECT * FROM test").then(function (recordset) {
          console.log(recordset);
          conn.close();
      })
      .catch(function (err) {
          console.log(err);
          conn.close();
      });       
  })
});

three is that full error msg

[nodemon] starting node server.js Express server listening on port NaN

in %s mode on port 3000

C:\Users\User\Documents\dbtest\node_modules\tedious\lib\ntlm-payload.js:44

var bufferLength = 64 + domain.length * 2 + username.length * 2 + lmv2len + ntlmv2len + 8 + 8 + 8 + 4 + server_data.len gth + 4;

TypeError: Cannot read property 'length' of undefined at

NTLMResponsePayload.createResponse (C:\Users\User\Documents\dbtest\node_modules\tedious\lib\ntlm-payload.js:44:59) at new NTLMResponsePayload

(C:\Users\User\Documents\dbtest\node_modules\tedious\lib\ntlm-payload.js:23:22) at Connection.sendNTLMResponsePacket (C:\Users\User\Documents\dbtest\node_modules\tedious\lib\connection.js:981:21) at Connection.receivedChallenge (C:\Users\User\Documents\dbtest\node_modules\tedious\lib\connection.js:1641:21) at Connection.dispatchEvent (C:\Users\User\Documents\dbtest\node_modules\tedious\lib\connection.js:857:45) at Connection.processLogin7NTLMResponse (C:\Users\User\Documents\dbtest\node_modules\tedious\lib\connection.js:1153:21) at Connection.message (C:\Users\User\Documents\dbtest\node_modules\tedious\lib\connection.js:1647:21) at Connection.dispatchEvent (C:\Users\User\Documents\dbtest\node_modules\tedious\lib\connection.js:857:45) at MessageIO. (C:\Users\User\Documents\dbtest\node_modules\tedious\lib\connection.js:751:18) at emitNone (events.js:86:13)

but connection it self worked with this

const Connection = require('tedious').Connection;
var connection = new Connection(config);
connection.on('connect', function(err) {
      if (err) {
        console.log(err);
      } else {
        console.log("server is connected to DB")
      }}
 );

looking for some help to solve this problem, thanks

Upvotes: 0

Views: 2388

Answers (1)

peteb
peteb

Reputation: 19428

Your problem is, the username property is user not userName, looks like node-mssql creates an alias for the userName which then gets assigned the variable username from userName in the tedious library. Then when trying to access the username.length, username is undefined resulting in your TypeError since undefined can't have a length. You can see this in the source on the tedious github.

var config = {
  user: 'user' , 
  password: 'pass', 
  domain: "AD",
  server: 'db.domain.local',
  database: 'test',
  port: 2222,
  debug: true,
  driver: 'tedious',
  options: {
    database:"test",
    instanceName : "instance"
  } 
}

Upvotes: 2

Related Questions