Node.js Real Time Chat: Problem while connecting to Mongodb

+1 Ronald McCarty · October 18, 2015
I've been trying to create a similar project as shown in one of these node.js tutorials.
#4 Inserting Data

Now, my MongoDB is installed all good, it works smooth. But while trying to execute this server.js file

var mongodb = require('mongodb').mongoClient;
var client = require('socket.io').listen(8080).sockets;

mongodb.connect('mongodb://127.0.0.1/chat' , function(err,db){
if (err) throw err;

client.on('connection', function(socket){
console.log('Someone has connected');
});
});


Its shows on my cmd
TypeError: Cannot read property 'connect' of undefined
    at Object.<anonymous> (C:\Users\S.W.A.T\Desktop\OMNI\server.js:4:8)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:117:18)
    at node.js:951:3
Please hit me back, i really need this thing to get work.

Post a Reply

Replies

Oldest  Newest  Rating
0 Ian Arbuckle · October 18, 2015
Are the modules in the correct directory?
0 Ronald McCarty · October 20, 2015
@Ian Arbuckle:
I think, you're right mate.
What directory you use for your modules and also is there any need to set environment variables?
0 Ian Arbuckle · October 20, 2015
This is how my file structure is setup

css
js
node_modules

  •       socket.io

  •       mongodb

  •       socket.io-logger



index.html
server.js
0 Ronald McCarty · October 21, 2015
I was using a similar directory.
Although i was not having socket.io-logger module.
Downloaded that too, still the same error.

You know, everything runs smooth, untill i type that third line of code.

mongodb.connect('mongodb://127.0.0.1/chat' , function(err,db){

if (err) throw err;
});


I believe error is in this line of code. Can you rectify?
HELP KIND MAN,HELP.
  • 1

Javascript

106,992 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators