0

Ran in to this problem while doing https://www.freecodecamp.com/challenges/store-data-in-mongodb

error message was

swyx:~/workspace $ learnyoumongo run find.js
/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/server.js:231
        process.nextTick(function() { throw err; })
                                      ^

TypeError: Cannot read property 'collection' of undefined
    at Exercise.<anonymous> (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/exercises/find/exercise.js:37:5)
    at next (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:271:17)
    at Exercise.end (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:277:5)
    at Workshopper.end (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:191:12)
    at Workshopper.done (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:323:19)
    at Exercise.<anonymous> (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:160:14)
    at /home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:147:16
    at /home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/exercises/find/exercise.js:20:21
    at /home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/mongo_client.js:238:20
    at /home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/db.js:242:14

there are other similar question on here but none of them were describing this exact problem

for reference this was the (valid) code i was running it on

var mongo = require('mongodb').MongoClient
var age = process.argv[2]

var url = 'mongodb://localhost:27017/learnyoumongo'

mongo.connect(url, function(err, db) {
  if (err) throw err
  var parrots = db.collection('parrots')
  parrots.find({
    age: {
      $gt: +age
    }
  }).toArray(function(err, docs) {
    if (err) throw err
    console.log(docs)
    db.close()
  })
})
1
  • i have found my own solution and posted below to help people in future Commented Nov 26, 2016 at 7:45

2 Answers 2

1

solution was my mongodb server experienced an unexpected shutdown overnight and wasn't even running. to start it back up again you have to recover (https://docs.mongodb.com/manual/tutorial/recover-data-following-unexpected-shutdown/) and then restart.

Sign up to request clarification or add additional context in comments.

2 Comments

You should add a pm2 service that will keep awake your server
ok. i dont know what pm2 is but i'll look it up when i have need for it
0

In version 2.x of the MongoDB native NodeJS driver you would get the database object as an argument to the connect callback:

mongo.connect(url, (err, db) => {
  // Database returned
});

But According to the changelog for 3.0 you now get a client object containing the database object instead:

mongo.connect(url, (err, client) => {
  // Client returned
  var db = client.db('mytestingdb');
});

The close() method has also been moved to the client so that you have to write client.close(); instead of db.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.