1

I cant connect to MongoDb using mongoose. I have installed MongoDB in my local system

//Import the mongoose module
    var mongoose = require('mongoose');

    //Set up default mongoose connection`enter code here`
    var mongoDB = 'mongodb://localhost/my_database';
    mongoose.connect(mongoDB, {
      useMongoClient: true
    });

    //Get the default connection
    var db = mongoose.connection;

    //Bind connection to error event (to get notification of connection errors)
    db.on('error', console.error.bind(console, 'MongoDB connection error:'));

    module.exports = mongoose.connection;

getting the error:

// MongoDB connection error: { MongoError: failed to connect to server [localhost:27017] on first connect
Samer Abu Gahgah
  • 751
  • 1
  • 9
  • 18

3 Answers3

3

I had the same problem. Seemingly using localhost in your connection string is the issue. Best just use 127.0.0.1 in place of localhost. In linux both work well but windows the localhost has issues

Example:

const DB_URL="mongodb://127.0.0.1/<db_name>"
Felix Orinda
  • 593
  • 4
  • 20
0

Did you start mongoDb with the command mongod Maybe run it on a differnt port with mongod --port 12345

H. Hakvoort
  • 161
  • 1
  • 2
  • 14
  • Well here is your issue "STORAGE [initandlisten] exception in initAndListen: 29 Data directory C:\data\db\ not found." – H. Hakvoort Oct 12 '17 at 14:49
  • I run mongod.exe. Following is the error.. MongoDB starting : pid=6652 port=27017 dbpath=C:\data\db\ 64-bit host=DESKTOP-2T8MJHS targetMinOS: Windows 7/Windows Server 2008 R2 l [initandlisten] distarch: x86_64 [initandlisten] target_arch: x86_64 [initandlisten] options: {} [initandlisten] exception in initAndListen: 29 Data directory C:\data\db\ not found., terminating [initandlisten] shutdown: going to close listening sockets... [initandlisten] shutdown: going to flush diaglog... [initandlisten] now exiting [initandlisten] shutting down with code:100 –  Oct 12 '17 at 14:51
  • Can you follow the instructions from this post? https://stackoverflow.com/questions/29749949/how-to-start-mongo-db-on-windows – H. Hakvoort Oct 12 '17 at 14:54
  • thats a perfect fix. I have an another error while posting data. (node:10100) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html –  Oct 12 '17 at 15:05
  • You can ignore that error, with a callback it should be working fine. – H. Hakvoort Oct 13 '17 at 17:15
0

Hareesh, you need to test your code, more specifically, your connection between Mongoose and Mongo. Mongoose is just a library, it does not automatically connect to Mongo which is why you have the code above to tell it to do so, but you need to test that.

Create a test directory at the root of whatever it is you called this project. Inside of the test folder, create a test_helper.js file. Inside of it, you are going to write the code above, but refactor it into the ES6 model in this manner.

//Import the mongoose module
const mongoose = require(‘mongoose’);

//Set up default mongoose connection`enter code here
mongoose.connect(‘mongodb://localhost/project_test’);
mongoose.connection
     .once(‘open’, () => console.log(‘Good to go!’))
     .on(‘error’, (error) => {
        console.warn(‘Warning’, error);
});

Hope this helps.

Daniel
  • 14,004
  • 16
  • 96
  • 156