Application not connecting to Mongoose database

I am having trouble connecting to my mongoose database. I just don't know if there is something wrong with my code or I need to install more mongoose packages. Or maybe reinstall everything. Can anyone tell me what the problem is?

Problematic lines:

var mongoose   = require('mongoose');
mongoose.connect('mongodb://node:node@novus.modulusmongo.net:27017/Iganiq8o'); // connect to our database

      

Is the application supposed to automatically connect to the database? Or do I need to run mongod in the background? My application works fine and connects to the server without these lines. And here is the error from the command line:

enter image description here

Can someone please explain what the error is and how can I fix it? I don't understand what is being said here. Many thanks.

Complete code:

// server.js

// BASE SETUP
// =============================================================================

// call the packages we need
var express    = require('express');        // call express
var app        = express();                 // define our app using express
var bodyParser = require('body-parser');

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8080;        // set our port


var mongoose   = require('mongoose');
mongoose.connect('mongodb://node:node@novus.modulusmongo.net:27017/Iganiq8o'); // connect to our database

// ROUTES FOR OUR API
// =============================================================================
var router = express.Router();              // get an instance of the express Router

// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
    res.json({ message: 'hooray! welcome to our api!' });
});

// more routes for our API will happen here

// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);

// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);

      

+3


source to share


4 answers


It looks like you used this blog as a link. I did this too and it was not connecting to my Modulus database as I expected, I double-checked the user and password to make sure nothing was wrong and tried to connect using the mongo shell from my machine:

mongo jello.modulusmongo.net:27017/your_url -u <user> -p <pass>



And it worked, so I was puzzled and found that the solution to this problem is to update in mongoose to 3.8.0

instead of the one used 3.6.13

, and it works flawlessly, it connects to the module database, although I don't know, I know what is happening or why this is happening , it must be something with a mongoose.

+5


source


The problem is that the database your application is trying to connect is using a different combination of user passwords. You need the right user / pass combination.

Your code will work without those two lines, but your applicator won't have DB support.



Optionally, you can run mongodb locally. The standard way is to run monogod.exe and create your database and change mongoose.connect('mongodb://node:node@novus.modulusmongo.net:27017/Iganiq8o')

to mongoose.connect('mongodb://localhost/<your-db>')

. Note that the db has no security (user / password) in this case.

+2


source


The problem is when you are trying to connect a database that is not in your MongoDB. I also faced the same problem and solve it like this:

  • Mongo database created.
  • Changed path to mongoose.connect('mongodb://localhost:27017/db_name');

  • Launched MongoDB using mongo command.
+1


source


Authentication error in mongoDB "MongoError: auth failed".

Maybe user: pass 'node: node' didn't exist in your local database, check if this user exists in your mongoDB or create a specific one with user: pass you want.

See getUsers docs of mongoDB

Good luck!

0


source







All Articles