Mongodb connection failed in local with node version 18.12.0? - node.js

I am seeing one issue with node version 18.12.0 and mongodb 6.0.2. I already build a nestjs application with mongodb. Here I use #nestjs/mongoose(v- 9.0.2) and mongoose (v-6.7.0)
Here I can see that when I upgrade node js to latest lts version then I am not able to connect to mongodb. It show an error like unable to connect to database.
But When I downgrade to node version 16.18.0 then it working fine. My question is that you guys already face this issue or I am only person getting this issue. If you know that then actually where is the problem occurred?
Here is my connection code-
MongooseModule.forRoot("mongodb://localhost:27017/nekmart", {
connectionFactory: (connection) => {
connection.plugin(slug, { number: true });
return connection
}
}),

Had same problem after upgrading to NodeJs 18.12.1; followed other blogs/comments and apparently
changing the Uri from mongodb://localhost:27017/test_db to mongodb://127.0.0.1:27017/test_db works.
Without getting into the specifics reasons it appears that localhost is rejected due to some changes in NodeJS.

Related

MongoDB connection problems

I am trying to connect MongoDB with node.js but whenever I try I am getting this error. (MongoDB is started).
[terminal]JavaScript code
I tried to restart MongoDB.
I found the solution, by replacing the URI from (mongodb://localhost:27017) to
(mongodb://0.0.0.0:27017).
And it worked for me.

Knex: Timeout acquiring a connection

Since today, I get the following error when I try to locally connect to a postgres database (v 12) using knex.js.
Unhandled rejection TimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
This happens on a project I've been working on for a year without any problems. Trying to isolate the issue, I created a new database with one table. When running the following lines of code, I get the same error:
const knex = require('knex');
const db = knex({
client: 'pg',
connection: 'postgresql://postgres:postgres#localhost/a_test',
pool: {
min: 0,
max: 10,
},
});
db.from('test_table')
.select(['id'])
.then(r => {
console.log(r);
});
I have no clue what might cause this. A couple of weeks ago everything worked fine and I didn't change anything in the meantime. I run postgres locally with postgresapp and when I connect to the database using psql, everything works fine. Any ideas where I could look to resolve this?
The problem
Nodejs V14 Made some breaking changes that affected the pg module! Which made it exit directly at connect() call.
One can know by downgrading to v13! (I call it the v14 HELL)! Which was a solution in the past!
A fix for pg was written in pg v8.0.3.
Fix for v14
If you are using postgres! With nodejs v14 and above ! Make sure to use the driver module pg at version >=8.0.3! And better upgrade to the latest
npm install pg#latest --save
If you are not using postgres! Try to update your DB driver! It may be the same! Also try with nodejs V13. To confirm it's the same problem! (V14 HELL)
What did happen in v14
If like me you like to know the details and what did happen !?
With node V14! Some breaking changes happened on the api! Also many things were changed! Including Openssl version!
For postgres! And pg module! The problem was as described in this comment per this thread:
The initial readyState (a private/undocumented API that
pg uses) of net.Socket seems to have changed from 'closed' to 'open'
in Node 14.
It’s hard to fix with perfect backwards compatibility, but I think I
have a patch that’s close enough.
And as per this PR!
You can see the changes in this diffing
In short as mentioned! The api for onReady changed for a net.Socket !
And the implemented solution was to not use onReady at all!
And as per this
Connection now always calls connect on its stream when connect is called on it.
In the older version the connect was called only if the socket is on closed state! readyState usage is eliminated!
Check this line
You can understand!
Depending on the implementation! Many things may or not be affected by those core changes!
Nodejs v14 relevant change
And because i wanted to see where the change happen! Here you go
https://github.com/nodejs/node/pull/32272
One can check the log of changes too:
https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V14.md
Detailed Why + exit and no logging error
Also to mention the breaking changes! Made pg make the process exit at connect() call. And that's what made it exit! And logging was to be seen!
In more detail for this! Here how it happened! Sequelize have the postgres dialect implementation! Which use pg! And pg client! create a connection! The connection have a connect event! When it connect it emit it! And because node v14 change the behavior of a stream to starting with open! The stream connection is skipped! Because of the readyState check (expected as close but it became open instead!)! And the stream is taken as connected (else block)! Where it is not! And the connect event is emitted directly! When that happen! The client either will call requestSsl() or startup() method of the connection object! And both will call this._stream.write. because the stream is not connected! An error happen! This error is not catch! Then the promise in sequelize driver! Will stay unresolved! And then the event loop get empty! Nodejs by default behavior just exit!
You can see the step through lines of code:
Sequelize pg adapter will call pg client to create a connection and the promise
pg client call connect on a connection object
pg connection connect() call and emit connect! Thinking the stream is connected because of V14 change
pg client connect event catched and callback run! requestSsl() or startup() will be run
One of the method get run and stream.write will be called (requestSsl(), startup())
Stream Error (not catched)
Promise in sequelize postgres adapter! Still unresolved!
event loop empty => Nodejs => Exit
Why nodejs exit (unresolved promises)
https://github.com/nodejs/node/issues/22088
Node exits without error and doesn't await promise (Event callback)
what happens when a Promise never resolves?
Looks like with Node 14 newer (>8.0.3) pg driver version should be used. https://github.com/knex/knex/issues/3912
It is a fact that this error can be caused by very many issues, today I found out a new one the hard way after scrolling up and down countless threads like this one to no avail.
when setting up the pool, there knex allows us to optionally register afterCreate callback, if this callback is added it is imperative that you make the call to the done callback that is passed as the last parameter to your registered callback or else no connection will be acquired leading to timeout.
.....
pool: {
afterCreate: (conn, done) => {
// .... add logic here ....
// you must call with new connection
done(null, conn);
},
}
.....
I just upgraded my psql using npm install pg#latest --save and my knex now is working.
Turns out it was the problem was node v14. When I use v13 or earlier it works.
"express": "^4.16.2",
"knex": "^0.14.2",
"objection": "^2.1.3",
"pg": "^8.0.3",
and npm install
i fixed my problem (end of the 4 day)

How to use the pg node package in angular

Situation
Hi, I'm quite new to Angular, I've been doing some projects following tutorials, which then lead me to try to start my own project to practice my Postgres and newly acquired Angular "skills".
I am trying to do a webapp that connects to a postgres DB using the node pg module.
(I know sequelize is a thing and it seems to work better than pg but AFAIK sequelize doesn't let you run pure postgres commands through it) Please correct me if I am wrong about this
The problem
This is where I get stuck, I am trying to follow the instructions from the docs but it doesn't seem to work correctly.
I have tried:
const { Client } = require('pg');
import { Client } from 'pg';
Also tried importing it in the .angular-cli.json in the scripts array
All of these fail with errors similar to this
ERROR in ./node_modules/pg/lib/connection-parameters.js Module not found: Error: Can't resolve 'dns' in '[...]\node_modules\pg\lib'
ERROR in ./node_modules/pg/lib/native/client.js Module not found: Error: Can't resolve 'pg-native' in '[...]\node_modules\pg\lib\native'
But nothing seems to work properly. Am I doing this completely wrong?
Also, pretty dumb question. I believe angular does everything on the client side, this is a HUGE security risk for DB access in prod. If that is true, is there a way to write server-side .ts services? or are services server-side?
You could write your serverside code in node using compiled ts, but probably not with angular.

Mongoose calls hangs

I haven't worked on my PC for few days.
Suddenly all the calls to mongo via mongoose hangs up, the callbacks are not called.
I checked that my call to .connect works, and that the connection state is 1 (connected).
I also made sure mongo service is running on localhost and the appropriate port 27017, and I can use the mongo console and query the db manually.
I also scanned the Internet for solutions but all I found was 'check that you're actually connected', and I verified that already.
Mongoose version 2.15.0, mongo version 2.4.9 and node js version is 4.4.2.
I fixed it.
Problem was duplicate references to the mongoose module.
I had a mongoose reference locally (which was connected), but my schema was present higher in the node_modules hierarchy, and it have used another mongoose instance which had no connection.
Once I removed the duplicate mongoose modules (npm uninstall mongoose one of them) it worked.
Above solutions didn't work for me so I fixed with following solution.
I had same issue where my db calls used to hang with no invocation to my callbacks or the promise resolution.
The problem was I used "createConnection()" to establish the connection with the db. But it didn't work perfectly.
Instead using "connect()" and "connection" imports worked.
Here is the sample code. Hope this helps.
import { connect, connection } from "mongoose";
const mongoUri = `mongodb://${my_mongo_host_&_port}`;
connect(mongoUri, {}); //to connect to my standalone db
//"connection" to listen to events
connection.on("connected", () => {
console.log("MongoDB connection established!", mongoUri);
});
I am working with "mongoose": "^6.6.1".

Auth failed, code 18 when connecting to MongoLab database

I'm trying to connect to a MongoLab database but keep getting the following error on connection:
{ [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: 'auth failed', code: 18 }
The code I'm using to connect is:
var mongoose = require("mongoose");
mongoose.connect("mongodb://username:password#ds061474.mongolab.com:61474/apitest");
mongoose.connection.on('error', function (err) {
console.log(err);
});
When I connect using the shell, I have no problems whatsoever. What am I doing wrong?
I have encountered similar problem when connecting the mongo db using mongoose. After exploring a while I found mongoLab is using SCRAM-SHA-1 authentication.
Refer to the question below I tried to upgrade my mongoose to V4.1.11, and then it works for me
Authentication in mongoose using SCRAM-SHA-1
I faced the same issue while I try to import data from the locale to server.
Those 2 parameters can be important, it worked after I put them:
--authenticationMechanism 'MONGODB-CR'
--authenticationDatabase "admin"
Be careful about the auth mechanism, can be a different one. Check this part of documentation: https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption-mongoimport-authenticationmechanism
Had this error myself, turns out I did two things incorrectly (thanks Idos):
Used the mongolab.com username instead of the database one.
Tried to connect to a mongo 3.4 database using a 2.6 shell provided through Ubuntu's repositories. mongo --version to check.
Follow the instructions from this MongoDB page to add their keys and repositories to your APT sources in order to upgrade and keep your MongoDB installation updated going forward.
i had a similar error in that case. put authSourse=admin and ssl=true to your connection
e.g
mongodb://username:password#ds061474.mongolab.com:61474/apitest?authSourse=admin&ssl=true

Resources