"events.js:141 throw er; // Unhandled 'error' event" || anybody knows how to fix?



Im new to c9, I have this error that I spent 2 days researching and trying to fix but it wont work!

throw er; // Unhandled ‘error’ event
MongoError: failed to connect to server [localhost:27017] on first connect
at null. (/home/ubuntu/workspace/YelpCamp/v1/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:326:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null. (/home/ubuntu/workspace/YelpCamp/v1/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:270:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket. (/home/ubuntu/workspace/YelpCamp/v1/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:175:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1269:8)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)

From what I understood is that c9 is trying to listen via a port that is already being used, however i found no solution on how to fix it.

I restarded c9, killed processers, tried simpler codes and no matter what I do it just wont work.

If anybody faced this error before PLEASE help me out.



I think it is the node version, not your code,
Try uninstalling node, nodejs and npm using:
sudo apt-get remove nodejs node npm
And reinstalling through here:
curl -sL https://deb.nodesource.com/setup_7.x sudo -E bash - sudo apt-get install -y nodejs

Hope this Helps :smile:


After going through a sea of wrong answers, I finally figured it out.

This error happened to me because I left mongo running and it crashed. Mongo locks the DB not allowing to start again until the lock is removed.


1.Locate mongo.lock (most likely will be found in data/mongo.lock )
2. rm mongo.lock
3. locate mongod
run mongod ( ./mongod )
now mongo should work and your code should work too

Hope this helps everybody