ftdeveloper
ftdeveloper

Reputation: 1093

My express application could not connect to mongodb

Hi i connect my mongodb with command prompt on windows. But i could not with express application. I read too many blog post about node,express and mongodb but could not find the problem. So i need advise.

My database folder : c:/data/db My mongodb folder: c:/mongodb

In command prompt i type:cd bin mongod Then i type: mongo and it says : enter image description here

And i can do whatever i want with command prompt.

But when i try to connect mongodb from my node express application i fail always. My code is shown below:

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:51241/ecmarketing');
var ObjectID = require('mongodb').ObjectID;
var collection = db.usercollection.findOne();
res.render('index', { title: 'index' });
};

Always i get:

enter image description here

package.json:

{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.4",
"ejs": "~0.8.4",
"ejs-locals": "~1.0.2",
"mongodb": "~1.3.19",
"monk": "~0.7.1"
}
}

I installed package with npm install. Also i created ecmarketing file in db folder. Thats all.

Upvotes: 0

Views: 348

Answers (1)

robertklep
robertklep

Reputation: 203304

A few comments:

  • are you sure your MongoDB is running on port 51241? The normal port is 27017;
  • with monk, you need to explicitly get a reference to the collection:

    var collection = db.get('usercollection');
    
  • you don't have to create a folder for the database yourself, MongoDB will do that for you;

Upvotes: 1

Related Questions