0

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

Vladimir Zaguzin
  • 191
  • 5
  • 22
  • What is the value and data type you are using for the server/serversIP value? In other node connection string examples, this appears to be the FQDN in string format. – Jacob H Jan 30 '18 at 14:30
  • @JacobH in my case I had to split servers IP address and instance name when I started it with just tedious; and it had no difference with mssql-node. I had it as server:127.0.0.1\\instance (replace 127... with a real one) and as a separate options – Vladimir Zaguzin Jan 30 '18 at 14:40
  • As the instructions note, you shouldn't pass the instance to the server property. You are already passing the instance to the instance property. The instructions also say not to use the server IP and to use the FQDN. So I would start by reviewing the documentation since that seems to be your problem. – Jacob H Jan 30 '18 at 14:41

1 Answers1

2

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"
  } 
}
peteb
  • 18,552
  • 9
  • 50
  • 62
  • 1
    Good spotting there. – Jacob H Jan 30 '18 at 14:47
  • @JacobH, i just saw it and it should be just User: ...; with pure tedious it was userName – Vladimir Zaguzin Jan 30 '18 at 14:54
  • @VladimirZaguzin yeah it should be `user`, looks like node-mssql creates an alias for the `userName` which then gets assigned the variable `username` from `userName` in the tedious library. You can see this in the [source on the `tedious` github](https://github.com/tediousjs/tedious/blob/cf92626e837160a835b63acc235f16ba78ac221c/src/ntlm-payload.js#L27) – peteb Jan 30 '18 at 14:59