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.