3gwebtrain
3gwebtrain

Reputation: 15303

Nodejs - using "mongodb" - module not connecting to server and keep throwing error

As a new node developer, I am not able to fix my data base connectivity problem. i am using expressjs for my app development, as well i use the "mongodb" module to connect with my "mongodb" - but i always getting error like this: - what would be the issue?

My error message :

bsd-pcs2029048-vijaya-2:myNodeApp mohamedarif$ node app.js
========================================================================================
=  Please ensure that you set the default write concern for the database by setting    =
=   one of the options                                                                 =
=                                                                                      =
=     w: (value of > -1 or the string 'majority'), where < 1 means                     =
=        no write acknowledgement                                                       =
=     journal: true/false, wait for flush to journal before acknowledgement             =
=     fsync: true/false, wait for flush to file system before acknowledgement           =
=                                                                                      =
=  For backward compatibility safe is still supported and                              =
=   allows values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}]      =
=   the default value is false which means the driver receives does not                =
=   return the information of the success/error of the insert/update/remove            =
=                                                                                      =
=   ex: new Db(new Server('localhost', 27017), {safe:false})                           =
=                                                                                      =
=   http://www.mongodb.org/display/DOCS/getLastError+Command                           =
=                                                                                      =
=  The default of no acknowledgement will change in the very near future                =
=                                                                                      =
=  This message will disappear when the default safe is set on the driver Db           =
========================================================================================

events.js:71
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: listen EADDRINUSE
    at errnoException (net.js:770:11)
    at Server._listen2 (net.js:910:14)
    at listen (net.js:932:10)
    at Server.listen (net.js:998:5)
    at Function.app.listen (/Users/mohamedarif/Sites/myNodeApp/node_modules/express/lib/application.js:533:24)
    at Object.<anonymous> (/Users/mohamedarif/Sites/myNodeApp/app.js:32:5)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

here is my app.js file:

var http    = require("http"),
    express = require("express"),
    stylus = require("stylus"),
    myEmployee = require("./employee").myEmployee,
    app = express();

app.set('view engine', 'jade');
app.set('views', './views');
app.set('port', process.env.PORT || 1337);

app.use(stylus.middleware({
    src : __dirname + '/styles',
    dest : __dirname + '/styles',
    debug : true,
    force:true
}));

var employeeProvider = new myEmployee('localhost', 27017); // where i am initiating!

app.use(express.static(__dirname + '/styles'));
app.use(express.static(__dirname + '/css'));

app.get('/', function (req, res, next){
    res.end("<h1>Sample Tittle</h1>");
    next();
});

app.get("/sample", function(req, res) {
    res.render('index');
})

app.listen(1337); 

here is my "myEmployee.js" :

var 
    Db = require("mongodb").Db,
    Server = require("mongodb").Server,
    Connect = require("mongodb").Connect,
    BSON = require("mongodb").BSON,
    ObjectID = require("mongodb").ObjectID;

myEmployee = function (host, port) {
    this.db = new Db('myEmployee', new Server(host, port, {safe : false}, {auto_reconnect : true}, {}));
    this.db.open(function(){});
} 

exports.myEmployee = myEmployee;

Any one help me to sort this issue please?

Upvotes: 0

Views: 1155

Answers (1)

Nate
Nate

Reputation: 19030

There are two unrelated problems.

Problem #1: The way you are connecting to MongoDB is no longer recommended.

With {safe: false} there is a possibility that data written to your database could be lost. For example, if there was an error at the server side, you would not know. Instead, you could do something like this:

this.db = new Db('myEmployee',
    new Server(host, port, {auto_reconnect : true}), {w:1});

Note the {w:1}. This means write commands will not return successfully until at least one MongoDB server in your cluster acknowledges the write. And if there’s a problem, it will return an error.

But there’s a better way to connect to MongoDB. See MongoClient or how to connect in a new and better way. Using that, your connect would look like:

// at the top of your file:
var MongoClient = require('mongodb').MongoClient
  , Server = require("mongodb").Server
  // any other stuff you need to require()
  ;

// later, in your constructor or init function:
this.mongoClient = new MongoClient(new Server(host, port));

Note that MongoClient always acknowledges writes to the database, so the above-mentioned problem goes away.

Problem #2: The EADDRINUSE error just means you have something else already listening on port 1337. Most likely it’s another instance of your app running in another terminal or console window. You can only have one instance running at a time on a given port number.

Upvotes: 2

Related Questions