0

I am following MEAN stack course from edx and trying to get set up. Here is the index.js file that I am trying to run:

var mongodb = require('mongodb');

var uri = 'mongodb://localhost:27017/example';
mongodb.MongoClient.connect(uri, function(error, db){
    if (error) {
        console.log(error);
        process.exit(1);
    }

    db.collection('sample').insert({x:1}, function(error, result){
        if(error){
            console.log(error);
            process.exit(1);
        }

        db.collection('sample').find().toArray(function(error, docs){
            if (error) {
                console.log(error);
                process.exit(1);
            }
        });
    });
});

When I did node index.js in terminal I had error "Address already in use for socket: 0.0.0.0:27017 " which was solved thanks to this post unable to start mongodb local server answered by Sacha (answer 4). Then I got error " Cannot find module 'mongodb'", which was solved by installing mongodb globally and using doing npm link mongodb on my app folder. Now I do not recieve any error, rather I do not get anything. Just a cursor which is waiting for an answer I guess. I use OS X El Capitan.

Community
  • 1
  • 1
Haris Khan
  • 171
  • 1
  • 2
  • 10

1 Answers1

0

This code does nothing except inserting a document in a collection called sample. You need to check its result using Mongo client, otherwise it looks useless.

David Rissato Cruz
  • 3,347
  • 2
  • 17
  • 17