Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 722
user2456977 Avatar asked Nov 19 '14 07:11

user2456977


2 Answers

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.

like image 63
ranu Avatar answered Nov 15 '22 01:11

ranu


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

like image 45
Rohit Avatar answered Nov 15 '22 01:11

Rohit