user2456977
user2456977

Reputation: 3964

Application not connecting to Mongoose Database

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

The problematic lines are:

var mongoose   = require('mongoose');
mongoose.connect('mongodb://node:[email protected]:27017/Iganiq8o'); // connect to our database

Is the application supposed to connect to the database automatically? Or do I need to run mongod in the background? My application runs perfectly and connects to the server without those lines. And here is the error from the command prompt:

enter image description here

Can someone please explain what the error is and how I can fix it? I don't understand what it says here. Thanks so much.

Full 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:[email protected]: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);

Upvotes: 5

Views: 4788

Answers (4)

Arya Asokan
Arya Asokan

Reputation: 29

The problem looks like you are trying to connect a database which is not in your MongoDB. I also encountered the same problem and get solved by doing as follows:

  • Created a database in mongo.
  • Changed the path to
    mongoose.connect('mongodb://localhost:27017/db_name');
  • Started the MongoDB using the command mongo.

Upvotes: 1

ranu
ranu

Reputation: 652

Looks like you used this blog post as a reference. I did it as well, and it didn't connected to my Modulus database as I expected, I double checked the user and password to see if nothing was wrong and tried to connect using mongo shell from my machine with:

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

And it worked, so I was puzzled and discovered that what solves the problem is an upgrade in mongoose to 3.8.0 instead of the used 3.6.13 and it worked flawlessly, it connects to the Modulus database, although I don't know what happens or why it happens, it must be something with mongoose.

Upvotes: 5

Rohit
Rohit

Reputation: 2152

The problem seems to be that the DB your application is trying to connect has different username password combination. You need the correct user/pass combination.

Your code will work without those two lines, but your applicaiton will not have DB support.

Optionally you could proceed with running mongodb locally. A standard way would be to running the monogod.exe and creating your DB and changing the mongoose.connect('mongodb://node:[email protected]:27017/Iganiq8o') to mongoose.connect('mongodb://localhost/<your-db>') . Please note the db in this case has no security(user/pass).

Upvotes: 3

Kangcor
Kangcor

Reputation: 1189

The error is about authentication in mongoDB 'MongoError: auth fails'.

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

See getUsers docs of mongoDB

Good luck!

Upvotes: 0

Related Questions