Sabeer Sulaiman Sabeer Sulaiman - 6 months ago 56
Node.js Question

Cannot fetch from or insert data to mongodb using mongoose

I wrote a node web app and created a mongoDb database on my local system. I was using the following code to connect to local mongodb from node js

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db_name'); //local


And everything was working fine on my local machine. So I went on and created an mlab account and created a database. But when I tried to run the code by changing the connection string, connections are still established I believe. But the find and save requests are not invoking the callbacks, even no errors shows up. All requests are getting timed out.

var mongoose = require('mongoose');
mongoose.connect("mongodb://user:pass@ds036789.mlab.com:36789/db_name"); //mlab


Another thing I noticed is that I cannot ping ds036789.mlab.com. But TCP connections are succeeding when I tried the nc command

nc -w 3 -v ds036789.mlab.com 36789


I even tried deploying to azure. Which doesn't work either. Any help is much appreciated. Thanks.

EDIT:
Not being able to ping was due to the fact that I used azure hosting. It is expected. And I also found out that I get this error while trying to connect :

connection error: { [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: 'auth failed', code: 18 }


Credentials are correct though.

Answer

I figured out the issue, it wasn't an issue with the credentials. It was an issue with the mongoose version. The mongoose version I used didn't support the authentication. I had to remove the package and reinstall the latest version. with

node install mongoose@latest

Hope it helps someone. And thanks for the answers :)