Jjang
Jjang

Reputation: 11444

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.

Upvotes: 3

Views: 2640

Answers (2)

varad_s
varad_s

Reputation: 1192

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

Upvotes: 0

Jjang
Jjang

Reputation: 11444

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.

Upvotes: 10

Related Questions