Error: No event 'socketConnect' in state 'SentPrelogin' - node.js

I am trying to connect my SQL Server database with node.js using knex but I am facing issue
Error: No event 'socketConnect' in state 'SentPrelogin'
at Connection.dispatchEvent (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1281:26)
at Connection.socketConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1303:10)
at C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1145:12
at Socket.onConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connector.js:106:7)
at Socket.emit (events.js:314:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1131:10)
Emitted 'error' event on Connection instance at:
at Connection.dispatchEvent (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1281:12)
at Connection.socketConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1303:10)
[... lines matching original stack trace ...]
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1131:10)
[nodemon] app crashed - waiting for file changes before starting...
My code is
var knex = require('knex')({
client: 'mssql',
version:"7_1",
connection: {
user: 'sa',
password: 'Admin#123',
server: 'localhost',
database: 'Demo'
}
});
knex.select("*").from("Country")
.then(function (depts){
depts.forEach((dept)=>{ //use of Arrow Function
console.log({...dept});
});
}).catch(function(err) {
// All the error can be checked in this piece of code
console.log(err);
}).finally(function() {
// To close the connection pool
knex.destroy();
});

You need to add a missing dependency:
npm install --save tedious
As of knex v0.95.0 you'll need to use the tedious library instead of mssql when connecting to an MSSQL database. According to the knex upgrade instructions:
MSSQL driver was completely reworked in order to address the multitude of connection pool, error handling and performance issues. Since the new implementation uses tedious library directly instead of mssql, please replace mssql with tedious in your dependencies if you are using a MSSQL database.
Installing the package above should resolve your issue. I also had to set the encrypt option to false when connecting to my local database to avoid this error:
ConnectionError: Failed to connect to localhost:1433 - self signed certificate

Related

Connect to LocalDB\MSSQLLocalDB with Objection.js Knexfile

I am trying to connect to a LocalDB\MSSQLLocalDB SQLExpress instance in my Node.js application that uses Objection.js/Knex for the data layer.
When I try to run a migration it fails to connect to the database. But I can connect via SSMS without issue.
Here is my Knexfile.js connection settings
module.exports = {
development: {
client: 'mssql',
useNullAsDefault: true,
connection: {
server: '(LocalDB)\\MSSQLLocalDB',
user: 'localadmin',
password: 'password$',
database: 'database'
},
migrations: {
directory: './migrations',
tableName: 'knex_migrations'
},
seeds: {
directory: './seeds'
},
...knexSnakeCaseMappers()
}
};
When I run a migration it gives the following error:
Failed to connect to (LocalDB)\MSSQLLocalDB:1433 - getaddrinfo ENOTFOUND (LocalDB)\MSSQLLocalDB
Error: Failed to connect to (LocalDB)\MSSQLLocalDB:1433 - getaddrinfo ENOTFOUND (LocalDB)\MSSQLLocalDB
at Connection.socketError (D:\Code\backend\node_modules\tedious\lib\connection.js:1393:28)
at D:\Code\backend\node_modules\tedious\lib\connection.js:1153:21
at GetAddrInfoReqWrap.callback (D:\Code\backend\node_modules\tedious\lib\connector.js:195:16)
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:77:17)
I believe this is an issue with tcp/ip settings in SqlExpress but I cannot find an easy way to change those.
Is there any settings for the Knexfile that will allow for a SQLExpress database connection?
LocalDB only accepts named pipes connections. Use proper SQL Express and enable TCP connections

when I try to connect to mongodb I got these error

(node:9804) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(Use node --trace-deprecation ... to show where the warning was created)
Connection errorMongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
name: 'MongoNetworkError'
}]
keep your mongodb compass open then try running the code from your IDE.

Database is not running in mongodb atlas due to unhandled promise exception

My code is running fine in localhost with no warnings but when I'm changing my connection string to the string provided by mongodb atlas, following errors are showing which you can see in the below pic and my browser keeps circulating plz guide me what I'm doing wrong
(node:13700) DeprecationWarning: 'open()' is deprecated in mongoose >=
4.11.0,
use 'openUri() instead, or set the 'useMongoClient' option if using 'connect()' or 'createConnection()'. See
http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client
(Use 'node --trace-deprecation... to show where the warning was
created)
Server started on port 3000
Error [MongoError]: failed to connect to server [undefined:27017] on first connect [Error: getaddrinfo ENOTFOUND undefined
at GetAddrInfoReqwrap.onlookup [as oncomplete] (dns.js:67:26) {}] name: 'MongoError'
at Pool. (C:\Users\Umer\Desktop\node\Alhamdulillah, complete webapp\All Validations Completed\node_modules\mongodb-core\lib\topologies\server.js: 336:35)
at Pool.emit (events.js:315 :20) at Connection.
(C:\Users\Umer\Desktop\node\Alhamdulillah, complet e webapp\A11
Validations Completed\node_modules\mongodb-core\lib\connection\poo1
Lis:280:12)
screenshot
According to the problem that you're facing, it is because you're coding the deprecated method of connecting to the MongoDB.
Your database.js file seems okay but I would suggest that you use dotenv and not a separate file for your secrets
But in your app.js file, kindly completely change the way you're connecting to MongoDB.
const config = require('./config/database')
const options = {useUnifiedTopology: true, useNewUrlParser: true}
mongoose.connect(config.mongoURI, options).then(
()=> console.log('connected to mongodb'),
(reason)=> console.error(`Error: ${reason}`)
)
// All of your other code ahead.
Happy Coding!

Nodejs Bull queue not connecting to Azure

When changing the settings inside the new bull Queue object, I get an error in the console. When running Bull Queue locally the application works perfectly fine. As soon as I change the credentials to Azure, I get the error below. When running locally I run the redis-server but not when using the Azure credentials.
I have tried the example tutorial, on the Azure website, with nodejs and the redis npm package, and the Azure redis cache works perfectly fine. Therefore, I am left to believe that I am doing something wrong in the config. I have also tried adding "maxRetriesPerRequest" and "enableReadyCheck" to the redis object however, they have had no effect. I also make sure I execute the done function within the process function.
const queue = new Queue('sendQueue', {
defaultJobOptions: { removeOnComplete: true },
redis: {
port: env.AZURE_REDIS_PORT,
host: env.AZURE_REDIS_HOST,
password: env.AZURE_REDIS_PASSWORD
},
});
at Queue.<anonymous> (/Users/abc/Projects/Sean/dist/tasks/sendQueue.js:47:11)
at Queue.emit (events.js:208:15)
at Redis.emit (events.js:203:13)
at Redis.silentEmit (/Users/abc/Projects/Sean/node_modules/ioredis/built/redis/index.js:482:26)
at Socket.<anonymous> (/Users/abc/Projects/Sean/node_modules/ioredis/built/redis/event_handler.js:122:14)
at Object.onceWrapper (events.js:291:20)
at Socket.emit (events.js:203:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at processTicksAndRejections (internal/process/task_queues.js:77:11)
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:183:27)
Try to add configuration for TLS when using Azure redis cache. Should be the same config value as host. I did not manage to get a connection without it.
var notificationQueue = new Queue('notifications', {
redis: {
port: Number(process.env.REDIS_PORT),
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASS,
tls: {
servername: process.env.REDIS_HOST
}
}});

Ghost Blog - Unable to connect with mysql

After deploying the Ghost blogging platform with success, i tried to configure it to use Mysql instead of sqllite3 using this database section of their config page, which says:
Database
By default, Ghost comes configured to use an SQLite database, which
requires no configuration.
Alternatively Ghost can also be used with a MySQL database by changing
the database configuration. You must create a database and user first,
you can then change the existing sqlite config to something like:
database: { client: 'mysql', connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'ghost_db',
charset : 'utf8' }
}
So ok, the setup is straight forward. but i'm still unable to connect ghost with mysql. The error i receive after starting the platform using npm start --production is :
> ghost#0.6.2 start /var/www/ghost
> node index
Migrations: Database initialisation required for version 003
Migrations: Creating tables...
Migrations: Creating table: posts
ERROR: connect ECONNREFUSED
Error: connect ECONNREFUSED
at errnoException (net.js:905:11)
at Object.afterConnect [as oncomplete] (net.js:896:19)
--------------------
at Protocol._enqueue (/var/www/ghost/node_modules/mysql/lib/protocol/Protocol.js:110:48)
at Protocol.handshake (/var/www/ghost/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/var/www/ghost/node_modules/mysql/lib/Connection.js:98:18)
at /var/www/ghost/node_modules/knex/lib/dialects/mysql/index.js:105:16
at tryCatch2 (/var/www/ghost/node_modules/bluebird/js/main/util.js:53:21)
at Promise._resolveFromResolver (/var/www/ghost/node_modules/bluebird/js/main/promise.js:544:13)
at new Promise (/var/www/ghost/node_modules/bluebird/js/main/promise.js:84:37)
at Client_MySQL.acquireRawConnection (/var/www/ghost/node_modules/knex/lib/dialects/mysql/index.js:104:10)
at Object.create (/var/www/ghost/node_modules/knex/lib/pool.js:33:19)
at Object.Pool.createResource (/var/www/ghost/node_modules/knex/node_modules/generic-pool-redux/pool.js:288:12)
I'm not sure what could be wrong, since i have other applications using mysql working without any problems.
Thanks in advance.
connection refused = tcp connection was attempted, but nothing is listening on the port, or was explicitly denied. Unless you explicitly enabled TCP support in mysql (and have the correct ip/port), you should probably be using a local unix-domain socket instead.

Resources