0
votes

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

1
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
@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 optionsVladimir Zaguzin
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

1 Answers

2
votes

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"
  } 
}