简体   繁体   中英

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:node@novus.modulusmongo.net: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:

在此输入图像描述

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: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);

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.

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:node@novus.modulusmongo.net:27017/Iganiq8o') to mongoose.connect('mongodb://localhost/<your-db>') . Please note the db in this case has no security(user/pass).

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM