question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error in learn you mongo exercise 3

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
evanlucascommented, Jun 9, 2016

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

1reaction
mahdiASCcommented, Apr 17, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoDB - Excersise 3 (Complete Pain To Work With) - Node
I may have to learn another database framework/lib cause this one is just so error prone without a clear indication whats wrong.
Read more >
MongoDB Exercises, Practice, Solution - w3resource
The best way we learn anything is by practice and exercise questions ... Hope, these exercises help you to improve your MongoDB query...
Read more >
Chapter 3-New Issue - M001: MongoDB Basics
Going on with the exercise in chapter 3, I think I can now log into Atlas. However, when I run a query I...
Read more >
strange mongodb and mongoose error: not master and ...
That means you're trying to read from a secondary node in a replica set, you can only read from the primary node by...
Read more >
MongoDB Exercises - W3Schools
Good luck! Start MongoDB Exercises ❯. If you don't know MongoDB, we suggest that you read our MongoDB Tutorial from scratch.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found