Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose never connects to mongodb

I'm trying to connect to MongoDB using Mongoose on an Amazon EC2 Linux server.

Here's my code:

var mongoose = require('mongoose');
console.log("Attempting antyhing to do with mongoose"); //shown

var db = mongoose.connection;
db.on('error',console.error.bind(console,'db connection error:')); //not shown
db.once('open',function(){
    console.log("Successful connection to db!"); //not shown
});

mongoose.connect('mongodb://localhost:27017/local',function(err){
    console.log("some kinda connection made"); //not shown
    if(err)
    {
        console.log("err: "+err);
    }
});

Frustratingly, I'm not getting any errors from mongoose whatsoever, but nothing seems to show up.

There seem to be a lot of questions about no callback with mongoose and mongo.

Here's a couple that I've looked at that I don't think are the problem for me:

  • Listen for the callback quickly: Mongoose Connection I moved my db.on('open'... call to before my connect call in case of a race condition.
  • Is Mongo running? Mongoose connect method fails on simple Node Server. Express, Mongoose, Path Yes, and on port 27017

Also for reference I'm following this tutorial: https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4

One thing I am doing that I'm worried about is I've split my code up into multiple files. So this mongoose connection code is being called from a app/models/host.js (or bear.js in tutorial) file. Let me know if posting the other files would be helpful.

like image 775
hubatish Avatar asked Sep 10 '16 17:09

hubatish


1 Answers

I also faced the same issue.

Check that the Mongoose version you are using supports the MongoDb server version

Check compatibility on this link: http://mongoosejs.com/docs/compatibility.html

Change the version of Mongoose in package.json file accordingly.

Hope this helps!

like image 55
Mihir Mistry Avatar answered Sep 16 '22 18:09

Mihir Mistry