tediousjs / tedious

Node TDS module for connecting to SQL Server databases.
http://tediousjs.github.io/tedious/
MIT License
1.58k stars 439 forks source link

[QUESTION] Error: Cannot read properties of null (reading 'lookup') #1648

Closed ModuleHunter closed 3 months ago

ModuleHunter commented 3 months ago

Question No matter what settings I use, tedious outputs the above error message when I want to connect to a MS SQL Server. It does not matter whether it is a remote server or a local server. I checked all possible connection settings (TCP/IP) in Sql Server Configuration Manager and experimented with various values (and restarted the server), but no luck. SQL Logins are also enabled. I installed NodeJS (20.16.0.0) only a few days ago on a fresh Windows 10. I also tried the 'mssql' extension but it outputs the same error message, too. I even added NodeJS as exception to Windows' firewall. I tried 'msnodesqlv8' as driver and started MS SQL Server Browser service, but just nothing helps.

This is my current connection code:

import { Connection, Request, TYPES } from 'tedious';

var config = {
    server: 'localhost', // <- also tried ::1 and 127.0.0.1
    authentication: {
        type: 'default',
        options: {
            userName: 'test',
            password: '<HIDDEN>'
        }
    },
    options: {
        //port: 1433, <- also tried instead of instanceName
        instanceName: 'SQLEXPRESS',
        encrypt: true, // <- does not affect anything
        database: 'master',
        trustServerCertificate: true, // <- does not affect anything when I use encrypt option
        debug: {
            packet: true,
            data: true,
            payload: true,
            token: true
        }
    }
};  
var connection = new Connection(config);
connection.on('debug', function(value) {
    console.log(value)
});
connection.on('connect', function(err) {
    console.log(err);
});
connection.connect();

Console output:

State change: Initialized -> Connecting
DocumentsView.vue:213 Error: Cannot read properties of null (reading 'lookup')
    at connection.ts:1966:32
    at Item.run (browser.js:153:14)
    at drainQueue (browser.js:123:42)

It should be noted that I can connect to the servers without problems in SQL Server Management Studio by using SQL Logins. I use Vue with Vite for a web application if that helps.

My package.json:

{
    "name": "eservices",
    "version": "0.0.0",
    "private": true,
    "type": "module",
    "scripts": {
         "dev": "vite",
         "build": "vite build",
         "preview": "vite preview"
    },
    "dependencies": {
      "@primevue/themes": "^4.0.3",
      "@types/node": "^22.1.0",
      "msnodesqlv8": "^4.2.1",
      "openai": "^4.54.0",
      "pinia": "^2.1.7",
      "primeicons": "^7.0.0",
      "primevue": "^4.0.3",
      "tailwindcss-primeui": "^0.3.4",
      "tedious": "^18.3.0",
      "vue": "^3.4.29",
      "vue-router": "^4.3.3"
    },
    "devDependencies": {
      "@vitejs/plugin-vue": "^5.0.5",
      "autoprefixer": "^10.4.20",
      "postcss": "^8.4.40",
      "tailwindcss": "^3.4.7",
      "vite": "^5.3.1",
      "vite-plugin-node-polyfills": "^0.22.0"
    }
}
mShan0 commented 3 months ago

Can you try running your connection code independently without going through Vue to see what happens? The error message is from Vue and not Tedious

ModuleHunter commented 3 months ago

Yeah, oddly, it works when I run it outside of Vue.

mShan0 commented 3 months ago

My best guess is that you're trying to read from the database before the connection is fully established in your Vue application

arthurschreiber commented 3 months ago

Isn't vue a front end framework? Tedious only works on Node.js, not in the browser.

ModuleHunter commented 3 months ago

Reading from the database actually happens after connection.connect(); command, but I did not include that code above. Anyway, running Tedious in Vue was most likely the issue because it just works in a separate Node instance. This assessment also explains why I could not find other users online having the same problem. Admittedly, I'm fairly new to Node.

mShan0 commented 3 months ago

connection.connect() initiates the connection asynchronously. You need to put the code that runs after the connection is established inside

connection.on('connect', function(err) {
    // Insert calls here
});
ModuleHunter commented 3 months ago

I can confirm that my code already runs inside the connect event. Everything works as desired now.

MichaelSun90 commented 3 months ago

Hi @ModuleHunter, thanks for confirming that it is working now. Will close this one for now.