ELOGIN error while connecting to the SQL server - node.js

I'm learning full stack web development and was trying to connect to SQL server from my backend Node.js. I was following an online video. While running the index.js file I get the below error -
ConnectionError: Login failed for user 'systemadmin'.
at C:\Users\akunjalw\Desktop\FullStack\server\node_modules\mssql\lib\tedious\connection-pool.js:70:17
at Connection.onConnect (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\connection.js:1038:9)
at Object.onceWrapper (node:events:640:26)
at Connection.emit (node:events:520:28)
at Connection.emit (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\connection.js:1066:18)
at Parser.<anonymous> (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\connection.js:2574:20)
at Object.onceWrapper (node:events:639:28)
at Parser.emit (node:events:520:28)
at Readable.<anonymous> (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\token\token-stream-parser.js:32:12)
at Readable.emit (node:events:520:28) {
code: 'ELOGIN',
originalError: ConnectionError: Login failed for user 'systemadmin'.
at Login7TokenHandler.onErrorMessage (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\token\handler.js:239:19)
at Readable.<anonymous> (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\token\token-stream-parser.js:26:33)
at Readable.emit (node:events:520:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Readable.push (node:internal/streams/readable:228:10)
at next (node:internal/streams/from:98:31)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ELOGIN',
isTransient: undefined
}
}
undefined
[nodemon] clean exit - waiting for changes before restart
The code is as follows
const sql = require("mssql");
const config = {
user: "systemadmin",
password: "R#jasthaan1212",
server: "localhost",
database: "ORG_EMPLOYEEDATA",
options: {
trustedconnection: true,
trustServerCertificate: true,
enableArithAbort: true,
instancename: "SQL2019",
},
port: 50685,
};
async function getEmployeeName() {
try {
let pool = await sql.connect(config);
let employeeData = await pool
.request()
.query("select * from dbo.EMPLOYEES_DATA");
return employeeData.recordsets;
} catch (error) {
console.log(error);
}
}
module.exports = { getEmployeeName: getEmployeeName };
const dboperations = require("./dboperations");
dboperations.getEmployeeName().then((result) => {
console.log(result);
});
Please let me know what exactly I'm missing here. I couldn't find the way to resolve this by searching in internet as well, may be I'm terrible at searching. Any help to resolve this is appreciated.

Related

ELOGIN Error with Node.js and SQL Server database Connection

I am trying to connect my SQL Database which is in sql server management studio to Node.js. Currently, the connection is being done locally on my machine. This is the error which I keep on getting:
undefined
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
ConnectionError: Login failed for user 'database access'.
at C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\mssql\lib\tedious\connection-pool.js:70:17
at Connection.onConnect (C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\tedious\lib\connection.js:1051:9)
at Object.onceWrapper (node:events:510:26)
at Connection.emit (node:events:390:28)
at Connection.emit (C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\tedious\lib\connection.js:1079:18)
at C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\tedious\lib\connection.js:2608:18
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ELOGIN',
originalError: ConnectionError: Login failed for user 'database access'.
at Login7TokenHandler.onErrorMessage (C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\tedious\lib\token\handler.js:245:19)
at Readable.<anonymous> (C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\tedious\lib\token\token-stream-parser.js:26:33)
at Readable.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Readable.push (node:internal/streams/readable:228:10)
at next (node:internal/streams/from:98:31)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ELOGIN',
isTransient: undefined
}
}
NOT CONNECTED
undefined
[nodemon] clean exit - waiting for changes before restart
At first I thought that this may be ocurring because I havent enabled windows firewall to allow my port: 1433 but even after doing that I kept on getting the same error message. I also made sure to enable the SQL Server Services in SQL Configuration Manager.
Here is my code:
dbConfig.js
const config = {
// dialect: 'mssql',
user: 'database access',
password: 'tronn6215',
// DESKTOP-NKHJ1GI
// SQLQuery1.sql
server: 'DESKTOP-NKHJ1GI',
database: 'Avtag Data',
options: {
trustServerCertificate: true,
trustedConnection: false,
enableArithAbort: true,
instancename: 'SQLEXPRESS'
},
// dynamic_port: 51582 (dynamic port num)
// port: 50381
port: 1433
//have enabled window firewall to allow static port 1433 for connection but still error
//Now error- code: 'ELOGIN'
}
module.exports = config;
dbOperation.js
const config = require('./dbConfig'),
sql = require('mssql');
const getEntryPoint = async() => {
try {
let pool = await sql.connect(config);
let entry = pool.request().query("SELECT * from ENTRY _POINT1")
console.log(entry);
console.log('CONNECTED')
return entry;
}
catch(error) {
console.log(error);
console.log('NOT CONNECTED')
}
}
module.exports = {
getEntryPoint
}
server.js
const express = require('express'),
dbOperation = require('./dbFiles/dbOperation'),
cors = require('cors');
dbOperation.getEntryPoint().then(res => {
console.log(res);
})

Problem connecting postgreSQL with Knex - assert.fail(`unknown message code: ${code.toString(16)}`)

I'm totally new to relational databases and I'm trying to build a node and express project with postgres using knex.
I'm getting the following error when trying to connect to postgres:
/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/parser.ts:202
assert.fail(`unknown message code: ${code.toString(16)}`)
^
AssertionError [ERR_ASSERTION]: unknown message code: 5b
at Parser.handlePacket (/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/parser.ts:202:16)
at Parser.parse (/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/parser.ts:103:30)
at Socket.<anonymous> (/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (node:events:394:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:199:23) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: undefined,
expected: undefined,
operator: 'fail'
}
I understand it's a connection problem, but I'm not sure why I'm getting this.
This is my connection code:
export const knex = require('knex')({
client: 'pg',
connection: {
host : 'localhost',
port : 3306,
user : 'notTheRealUser',
password : 'notTheRealPassword',
database : 'pgdb'
}
})
knex.raw("SELECT 1").then(() => {
console.log("PostgreSQL connected")
})
.catch((e: Error) => {
console.log("PostgreSQL not connected")
console.error(e)
})
And then I'm importing the Knex object on the different routes to make queries, like so:
import { knex } from '../db/db'
router.post('/register', async (req: Request, res: Response) => {
// Check if the email isn't already taken
try {
const emailIsTaken = await knex('users').where({ email: req.body.email })
if (emailIsTaken) return res.status(500).json('Email already used')
} catch (err) {
res.status(500).send(err)
console.error(err)
}
})
Full code can be found here: https://github.com/coccagerman/mixr-back
you are using MySQL port 3306,
PostgresQL uses port 5432

Error "ConnectionError [SequelizeConnectionError]: Failed to connect to localhost:1443 - Could not connect (sequence)" using sequelize-automate

I'm getting a connection error when trying to automatically create the db table models using sequelize-automate.
However, the connection I'm using for sequelize is working but it's in a different format and I'm not sure if sequelize-automate allows me to use the same format.
const Sequelize = require('sequelize');
const util = require('util');
const dbSequelize = require('../public/dbconfigSequelize.json');
let databaseUrl = util.format(
dbSequelize.server,
dbSequelize.username,
dbSequelize.password,
dbSequelize.host,
dbSequelize.database
)
function getConnection() {
console.log('creating a new connection');
return new Sequelize(databaseUrl, {
pool: {
max: 1
},
logging: console.log
});
}
function checkAuthenticate() {
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
}
const sequelize = getConnection();
checkAuthenticate();
module.exports = sequelize;
package.json
"scripts": {
"generate-sequelize-models": "sequelize-automate -c sequelize-automate.config.json"
}
sequelize-automate.config.json
{
"dbOptions": {
"database": "mydbname",
"username": "myusername",
"password": "mypassword",
"dialect": "mssql",
"host": "localhost",
"logging": false
},
"options": {
"type": "js",
"dir": "./server/models"
}
}
The error:
PS C:\Users\username\Documents\myelectronapp> npm run generate-sequelize-models
> myelectronapp#0.1.0 generate-sequelize-models C:\Users\username\Documents\myelectronapp
> sequelize-automate -c sequelize-automate.config.json
Database options: {"database":"mydbname","username":"myusername","password":"****","dialect":"mssql","host":"localhost","port":3306,"logging":false}
Automate options: {"type":"js","camelCase":false,"dir":"./server/models","emptyDir":false}
ConnectionError [SequelizeConnectionError]: Failed to connect to localhost:3306 - Could not connect (sequence)
at C:\Users\username\Documents\myelectronapp\node_modules\sequelize\lib\dialects\mssql\connection-manager.js:134:17
at tryCatcher (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\promise.js:547:31)
at Promise._settlePromise (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\promise.js:604:18)
at Promise._settlePromise0 (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\promise.js:649:10)
at Promise._settlePromises (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\promise.js:725:18)
at _drainQueueStep (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\async.js:93:12)
at _drainQueue (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\async.js:86:9)
at Async._drainQueues (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\async.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\async.js:15:14)
at processImmediate (internal/timers.js:456:21) {
name: 'SequelizeConnectionError',
parent: ConnectionError: Failed to connect to localhost:3306 - Could not connect (sequence)
at ConnectionError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\errors.js:13:12)
at Connection.socketError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connection.js:1288:56)
at C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connection.js:1114:21
at SequentialConnectionStrategy.connect (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connector.js:87:14)
at Socket.onError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connector.js:100:12)
at Socket.emit (events.js:311:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
message: 'Failed to connect to localhost:3306 - Could not connect (sequence)',
code: 'ESOCKET'
},
original: ConnectionError: Failed to connect to localhost:3306 - Could not connect (sequence)
at ConnectionError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\errors.js:13:12)
at Connection.socketError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connection.js:1288:56)
at C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connection.js:1114:21
at SequentialConnectionStrategy.connect (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connector.js:87:14)
at Socket.onError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connector.js:100:12)
at Socket.emit (events.js:311:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
message: 'Failed to connect to localhost:3306 - Could not connect (sequence)',
code: 'ESOCKET'
}
}
Check this out, this is not from sequelize-automate but quite likely your issue is not particular to that anyway, otherwise it would be well documented by others
https://github.com/typeorm/typeorm/issues/2133
can't help but notice the error in the title is not the same as the error in your trace above either... why is it using default port for mySQL instead of mssql
In my case the error appeared because I had wrong dialect: mssql instead of mysql.
const sequelize = new Sequelize('Users', 'admin', 'pswd', {
host: 'localhost',
port: 3307,
dialect: 'mssql'
});
Updating the dialect to mysql fixed it.

Unknown command when using promise-ftp in node

I'm trying to use a promise approach when interacting with my ftp server. I'm using the node package 'promise-ftp' v1.3.5.
The code I have is as follows:
const appFeedBuffer = Buffer.from('somedatastring', 'utf8');
var Client = require('promise-ftp');
var ftpClient = new Client();
ftpClient.connect({ host: 'myHost', user: 'me', password: 'mypassword' })
.then(() => {
return ftpClient.mkdir('newDir')
}).then(() => {
return ftpClient.put(appFeedBuffer, 'fileName')
}).then(()=> {
return ftpClient.rmdir('oldDir')
}).then(() => {
return ftpClient.rename('newDir', 'oldDir')
}).then(() => {
return ftpClient.end();
}).catch(err => {
console.log("something went wrong: " + err);
res.status(500).send('SERVER ERROR');
});
The problem is the put command - I get the error 'UNKNOWN COMMAND' which appears to come from the #icetee/ftp library, of which promise-ftp is a dependant. put is most certainly a valid command in the promise-ftp library!
Stack:
Unhandled rejection Error: Unknown command
at makeError (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:1128:13)
at Parser.<anonymous> (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:122:25)
at emitTwo (events.js:126:13)
at Parser.emit (events.js:214:7)
at Parser._write (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/parser.js:61:10)
at doWrite (_stream_writable.js:397:12)
at writeOrBuffer (_stream_writable.js:383:5)
at Parser.Writable.write (_stream_writable.js:290:11)
at Socket.ondata (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:298:20)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:597:20)
I can't find anyone else having this problem. What am I doing wrong?

Getting error while connecting database with nodejs

app.get('/', function (req, res) {
var config = {
//user: 'sa',
// password: '',
server: 'localhost',
database: 'TestDB',
debug: true
};
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from userInfo', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
//console.log(recordset);
sql.close();
});
});
});
I want to connect without user and password and Getting this error while executing through command prompt and there is some trusted connection query I can't find the please help
Error: Login failed for user ''.
at Connection.tedious.once.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:216:17)
at Object.onceWrapper (events.js:293:19)
at emitOne (events.js:96:13)
at Connection.emit (events.js:191:7)
at Connection.processLogin7Response (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:1148:16)
at Connection.message (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:1660:14)
at Connection.dispatchEvent (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:863:38)
at MessageIO.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:757:18)
at emitNone (events.js:86:13)
at MessageIO.emit (events.js:188:7)
code: 'ELOGIN',
originalError:
{ ConnectionError: Login failed for user ''.
at ConnectionError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\errors.js:12:12)
at Parser.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:507:33)
at emitOne (events.js:96:13)
at Parser.emit (events.js:191:7)
at Parser.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\token\token-stream-parser.js:54:15)
at emitOne (events.js:96:13)
at Parser.emit (events.js:191:7)
at addChunk (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:284:12)
at readableAddChunk (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:271:11)
at Parser.Readable.push (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:238:10) message: 'Login failed for user \'\'.', code: 'ELOGIN' },
name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1299:37)
at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:497:11)
at Request.query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1242:12)
at D:\Nodejs\UsersCreate\app.js:119:17
at _poolCreate.then.catch.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:269:7)
at process._tickCallback (internal/process/next_tick.js:109:7) code: 'ECONNCLOSED', name: 'ConnectionError' }
GET / 200 36.940 ms - -
Please help me on this and want to connect this database using window authentication
You can try with:
options: {
trustedConnection: true
}
First install NPM Package,
npm install msnodesqlv8
Then change your config variable as,
const sql = require('mssql/msnodesqlv8')
var config = {
server: 'localhost',
driver: 'msnodesqlv8',
database: 'TestDB',
debug: true,
options: {
trustedConnection: true
}
};

Resources