when I try to connect to mongodb I got these error - node.js

(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.

Related

Memcached refuse connection after node upgrade to v18.7.0

I am using memcached locally to store user sessions.
To work with memcached I use the npmjs.com/package/memcached
After upgrading nodes to version v18.7.0, the server stopped connecting to memcached at localhost:11211
However, when the address is changed to 127.0.0.1:11211, the connection proceeds normally, but an error still occurs during further work:
Error: connect ECONNREFUSED ::1:11211
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) {
[stack]: 'Error: connect ECONNREFUSED ::1:11211\n' +
' at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16)',
[message]: 'connect ECONNREFUSED ::1:11211',
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 11211
}
What could be causing this and how to fix it?
The problem was that memcached was configured to accept connections only via IPv4.
After adding in the settings for receiving connections via IPv6, everything worked.

Error: No event 'socketConnect' in state 'SentPrelogin'

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

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!

Can't connect to Mongo Atlas getaddrinfo ENOTFOUND [cluster name]

Using Botkit to create slack bot but can't connect to Mongo Atlas for storage. Nodejs backend. Using Botkit-storage-mongo
Code:
var Botkit = require('botkit');
var BotkitStorage = require('botkit-storage-mongo');
storage = BotkitStorage({ mongoUri: `mongodb+srv://<username>:<password>#<clustername>/test?retryWrites=true&w=majority`})
When I run the code I get this error:
UnhandledPromiseRejectionWarning: Error: MongoError: failed to connect to server [<cluster_name>] on first connect [Error: getaddrinfo ENOTFOUND <cluster_name>
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26) {
name: 'MongoError',
message: 'getaddrinfo ENOTFOUND <cluster_name>'
}]
Dependencies:
"dependencies": {
"botkit": "0.6.21",
"botkit-storage-mongo": "1.0.7",
"mongodb": "^3.5.0"
}
In Mongo Atlas,
my IP address is whitelisted.
Also tested allowing all IPs: 0.0.0.0/0.
Any ideas as to why I cannot connect?
This issue had nothing to do with botkit.
Needed to update connection driver version in mongo atlas dashboard.
Was Node.js 3.0 or later driver version. When I changed that to the Node.js 2.2.12 or later version, that connection string worked.

I can't seem to be able to start a nodeJS service

I'm trying to start a service:
c:\MongoUni\Ex3\chapter3>node index.js
express-session deprecated undefined resave option; provide resave option auth.js:48:37
express-session deprecated undefined saveUninitialized option; provide saveUninitialized option auth.js:48:37
Listening on port 3000!
I get this error
c:\MongoUni\Ex3\chapter3\node_modules\mongodb\lib\server.js:228
process.nextTick(function() { throw err; })
^
Error: connect ECONNREFUSED 127.0.0.1:27017
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
It looks like your app is trying to connect to mongodb at port 27017 but you don't have one running on your machine. Here are instructions on how to install it on Windows (which i'm guessing you're running based on the filepath) https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/

Resources