learnyoumongo: error in learn you mongo exercise 3

I’m running from c9.IO, whenever I try to run the selected file with the command “learnyoumongo run file.js” I get the below error.

I tried playing around with the code and I tried it with a blank file, the result is always the same…

/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/server.js:235
        process.nextTick(function() { throw err; })
                                      ^

TypeError: Cannot read property 'collection' of undefined
    at Exercise.<anonymous> (/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/exercises/find/exercise.js:37:5)
    at next (/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:260:17)
    at Exercise.end (/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:266:5)
    at Workshopper.end (/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:191:12)
    at Workshopper.done (/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:323:19)
    at Exercise.<anonymous> (/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:149:14)
    at /home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:136:16
    at /home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/exercises/find/exercise.js:20:21
    at /home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/mongo_client.js:269:20
    at /home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/db.js:226:14

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Comments: 16 (4 by maintainers)

Most upvoted comments

@ThomastheBicyclist you would need to run npm install mongodb whenever you see that error

Was having same issue and this helped: https://stackoverflow.com/questions/47662220/db-collection-is-not-a-function-when-using-mongoclient-v3-0/47662979

Essentially, the newer version of mongodb has changed how the .connect() method works. It no longer provides a db, but a Client object. You can extract the db from this object by referring to the db by name. For example:

const mongo = require("mongodb").MongoClient;
const url = "mongodb://localhost:27017/learnyoumongo";
const database = "learnyoumongo";
const ageThresh = parseInt(process.argv[2]);

mongo.connect(url, function (err, client) {
    if (err) throw err;
    const col = client.db(database).collection("parrots");
    col.find({
        age: {$gt:ageThresh}
    }).toArray(function (err, docs) {
            if (err) throw err;
            console.log(docs);
        })
    client.close();
});

Note how you close the client now, and not the db

i tried the following and it worked with me delete the database directory and recreate it, (care this will delete every database you have in this directory )

mkdir ./data
mongod --dbpath ./data

after this i did forget to add db.end(); after i am done so i add it and it work just fine after .

I’m getting the same error as well. If I run mongod myself (with options as shown above) I get an error about the server closing the connection.

Edit: Oops, my bad! Turns out that the problem was largely on my side. I did have to run mongod myself, which maybe should be stated more clearly in the exercise (unless you shouldn’t have to and it’s an error, then fixing it might be the better solution). The closing connection ended up being because I misplaced db.close(). There’s still definitely an issue here though, although a change to the exercise text might be all that’s needed to fix it.