Git Product home page Git Product logo

Comments (41)

midori0507 avatar midori0507 commented on August 27, 2024 18

Hey guys, the issue happen with latest version of mongodb, try to use another version, remove node_modules and
npm install [email protected]

from learnyoumongo.

mat-singh avatar mat-singh commented on August 27, 2024 6

After MongoDB 3.0, the hints and solutions are now obsolete. The main difference is, you can't specify the db name along with url, but have to use the client's db function to associate. Ex:
const mongo = require('mongodb').MongoClient
mongo.connect(url, function(err, client) {
const db = client.db('learnyoumongo');
// now use db as you would normally use
})
The link to solutions for latest version of MongoDB are present here:
#64

from learnyoumongo.

Maroo-b avatar Maroo-b commented on August 27, 2024 5

For anyone having problem
"TypeError: Cannot read property 'collection' of undefined"

You have to start mongodb server in another terminal with this command:
mongod --port 27017 --dbpath=./data --nojournal

Then verify the file.

I hope this will help anyone.

from learnyoumongo.

rulanfuse avatar rulanfuse commented on August 27, 2024 2

It seems it's necessary to run "npm install mongodb" before doing exercise 3.

from learnyoumongo.

bapinney avatar bapinney commented on August 27, 2024 2

I ran into this problem, too. At first I presumed there was just some error in the js file I was trying to verify. However, no matter what I commented out (even if I commented everything out and hand a blank file), the solution would still fail. It made me realize that it's probably not my code.

I finally did what mr-cjk mentioned he did in his post, above. I ran learnyoumongo again, selected the first exercise, and hit enter. Then, without doing anything else, I ran "learnyoumongo verify mongo-find.js" and exercise 3 finally passed (despite it failing before, despite I did not modify the .js file at all afterwards, and despite exercise 1 being the most recent exercise selected in learnyoumongo).

Edit: Just realized that, although my file for exercise 3 PASSES when I do those steps, learnyoumongo marks exercise 1 as passing and not exercise 3 (which would make sense since that was the last exercise I selected when I tried the method mr-cjk mentioned).

from learnyoumongo.

atan77 avatar atan77 commented on August 27, 2024 2

I had a similar problem, but finally realised that the square brackets in the verify command weren't necessary. Having come from learnyounode, I noticed the difference in verify commands and assumed that the square brackets were necessary. It might be worth removing the square brackets from the instructions.

from learnyoumongo.

cjknowles avatar cjknowles commented on August 27, 2024 1

Alright, I can get it to work but I need to go back the run Exercise 1 MONGOD again.

If I go straight into learnyoumongo and select the FIND exercise, and then learnyoumongo verify file it fails, with an empty response from mongo and an error...

module.js:338
    throw err;
          ^
Error: Cannot find module 'mongodb'

I know mongo is running as if I use systemctl status mongod I get status Active: active (running).

But if I go back and run Exercise 1 MONGOD again, and then try the verify on Exercise 3's solution... it works.

Not sure if this intended or a problem in the code for Ex3? If it is intended it will be worth noting that the exercises should run in one sequential session.

Also, to explain a bit of more of why I struggled. I usually read the problem and then try to fix it in isolation of verify, and then when I have what looks like a solution I'll run it against verify and see if I win -- hence the confusion with the parrot collection, I wanted to code against it as a target but it wasn't there.

from learnyoumongo.

EightArmCode avatar EightArmCode commented on August 27, 2024 1

I am having a lot of problems finishing these modules. It appears there is configuration/setup to be done every single time that is not clearly explained.

from learnyoumongo.

 avatar commented on August 27, 2024 1

I was having an issue with step 3 as well, until reading an answer on SO that db.close() needs to be inside a callback, like toArray. Posting link to SO here for others who may be stuck on this step.

https://stackoverflow.com/questions/31699962/simple-mongodb-query-find-item-age-10-learnyoumongo-find-function

from learnyoumongo.

evanlucas avatar evanlucas commented on August 27, 2024

The parrots collection is created by the exercise

from learnyoumongo.

evanlucas avatar evanlucas commented on August 27, 2024

I realized that the HINTS for the FIND problem could have been better. Went ahead and added additional info in 9def6d5.

from learnyoumongo.

cjknowles avatar cjknowles commented on August 27, 2024

I've tried 'show dbs' from the mongo shell after running the exercise; but I cannot see the collection or database.

Also tried cheating and just running the solution and it just gave a blank [] as the result.

The problem must be my setup?

from learnyoumongo.

evanlucas avatar evanlucas commented on August 27, 2024

Yea, the exercise deletes the collection on cleanup. After you connected via the mongo shell, did you run use learnyoumongo? After that, try show collections

from learnyoumongo.

evanlucas avatar evanlucas commented on August 27, 2024

Also, how did you run the solution?

from learnyoumongo.

cjknowles avatar cjknowles commented on August 27, 2024

I ran using 'node ... path to solution' and I think when I did this mongo created an empty learnyounode database.

I've just dropped that empty database and tried opening the exercise again, thinking that it may be blocking a fresh one being made; and then in a different window tried 'mongo, use learnyoumongo, show collections' and there is still no content.

from learnyoumongo.

evanlucas avatar evanlucas commented on August 27, 2024

Yea, there won't be any content because it gets cleaned up after the exercise is done running.

from learnyoumongo.

evanlucas avatar evanlucas commented on August 27, 2024

try using learnyoumongo verify file to verify the solution instead

from learnyoumongo.

evanlucas avatar evanlucas commented on August 27, 2024

strange, the mongodb module should be bundled so I am assuming that the error is being thrown from the file that you ran against verify?

from learnyoumongo.

cjknowles avatar cjknowles commented on August 27, 2024

I am verifying the solution like a good cheat...

~/.npm/learnyoumongo/1.1.4/package/exercises/find/solution/solution.js

from learnyoumongo.

jessecogollo avatar jessecogollo commented on August 27, 2024

@mr-cjk did you run "npm install mongodb"?
other thing are you can paste the code in a gist.

from learnyoumongo.

evanlucas avatar evanlucas commented on August 27, 2024

@EightArmCode can you give me some more details on the issues you are having?

from learnyoumongo.

nalexiou avatar nalexiou commented on August 27, 2024

I am also having trouble with the FIND exercise.

I run this command: learnyoumongo run [solution.js] and get the following error:
....
/home/ubuntu/.nvm/v0.10.35/lib/node_modules/learnyoumongo/exercises/find/exercise.js:37
db.collection('parrots').remove({}, function(err) {
^
TypeError: Cannot call method 'collection' of undefined
...

Am I supposed to create a solution.js file with the code? I tried this but did not seem to work (I even copied the solution of the exercise and it did not seem to make a difference).

If you can provide some insight/help, I would appreciate it.

Please feel free to let me know if you need additional information regarding the errors I get.

Thank you.

Nikolaos

from learnyoumongo.

evanlucas avatar evanlucas commented on August 27, 2024

Can you share the code you ran to get that error?

from learnyoumongo.

nalexiou avatar nalexiou commented on August 27, 2024

Hi Evan,

I actually was able to figure it out. Basically, I had to run the command at the terminal like this:

learnyoumongo run solution.js (once I got rid of the brackets from the solution.js filename, it worked!)

Maybe it would be helpful to note this somewhere.

-Nikolaos

ORIGINAL COMMENT.....
Yes, sure. Here is the code that I placed in solution.js:

var mongo = require('mongodb').MongoClient
var age = parseInt(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()
  })
})

from learnyoumongo.

SmileeYang avatar SmileeYang commented on August 27, 2024

Hi all, I'm new in Node.js and learning mongoDB just today morning, and here comes problem with exercise 3 also...
Following is my mongoFind.js

var mongo = require('mongodb').MongoClient
var url = 'mongodb://localhost:27017/learnyoumongo'
var age = parseInt(process.argv[2])

mongo.connect(url, function (err, db) {
    if (err) throw err
    db.collection('parrots').find({ 
        age: {
            $gt: +age
        } 
    }).toArray(function (err, documents) {
        if (err) throw err
        console.log(documents)
        db.close()
    })
})

And I run: learnyoumongo verify mongoFind.js
but always got

    /usr/local/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> (/usr/local/lib/node_modules/learnyoumongo/exercises/find/exercise.js:37:5)
        at next (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:260:17)
        at Exercise.end (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:266:5)
        at Workshopper.end (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:191:12)
        at Workshopper.done (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:323:19)
        at Exercise.<anonymous> (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:149:14)
        at /usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:136:16
        at /usr/local/lib/node_modules/learnyoumongo/exercises/find/exercise.js:20:21
        at /usr/local/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/mongo_client.js:269:20
        at /usr/local/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/db.js:225:14

Even I tried solution.js in ./learnyoumongo/exercises/find/solution/solution.js with learnyoumongo verify solution.js, always return above error message... Please help me! Thanks!!!

from learnyoumongo.

SmileeYang avatar SmileeYang commented on August 27, 2024

Hello @rulanfuse I've tried your suggestion and run npm install mongodb before verify Exercise 3 with my mongoFind.js but still got error message below

    /usr/local/lib/node_modules/learnyoumongo/exercises/find/exercise.js:37
    db.collection('parrots').remove({}, function(err) {
        ^

    TypeError: Cannot read property 'collection' of undefined
        at Exercise.<anonymous> (/usr/local/lib/node_modules/learnyoumongo/exercises/find/exercise.js:37:5)
        at next (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:260:17)
        at Exercise.end (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:266:5)
        at Workshopper.end (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:191:12)
        at Workshopper.done (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:323:19)
        at Exercise.<anonymous> (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:149:14)
        at /usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:136:16
        at Exercise.<anonymous> (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/filecheck.js:10:14)
        at FSReqWrap.oncomplete (fs.js:82:15)

I think maybe was mongodb install path issue, I ran npm install mongodb and it goes to

    > [email protected] install /Users/smilee_yang/Desktop/Project/side-project/Nodejs/Class/node_modules/mongodb/node_modules/mongodb-core/node_modules/kerberos
    > (node-gyp rebuild) || (exit 0)
    CXX(target) Release/obj.target/kerberos/lib/kerberos.o
    CXX(target) Release/obj.target/kerberos/lib/worker.o
    CC(target) Release/obj.target/kerberos/lib/kerberosgss.o
    CC(target) Release/obj.target/kerberos/lib/base64.o
    CXX(target) Release/obj.target/kerberos/lib/kerberos_context.o
    SOLINK_MODULE(target) Release/kerberos.node
    [email protected] node_modules/mongodb
    ├── [email protected] ([email protected], [email protected], [email protected], [email protected])
    ├── [email protected]
    └── [email protected] ([email protected], [email protected])

Is it? And then I try again, but this time learnyoumongo return another error message...

    /usr/local/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> (/usr/local/lib/node_modules/learnyoumongo/exercises/find/exercise.js:37:5)
    at next (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:260:17)
    at Exercise.end (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:266:5)
    at Workshopper.end (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:191:12)
    at Workshopper.done (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:323:19)
    at Exercise.<anonymous> (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:149:14)
    at /usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:136:16
    at /usr/local/lib/node_modules/learnyoumongo/exercises/find/exercise.js:20:21
    at /usr/local/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/mongo_client.js:269:20
    at /usr/local/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/db.js:225:14

Oh my god... Please help me...

from learnyoumongo.

Seryal85 avatar Seryal85 commented on August 27, 2024

I have same problem

from learnyoumongo.

ashtonian avatar ashtonian commented on August 27, 2024

I have similar problem.

I've ran

npm install mongodb

and tried running

learnyoumongo verify solution.js 

only to find

TypeError: Cannot read property 'collection' of undefined
    at Exercise.<anonymous> (/usr/local/lib/node_modules/learnyoumongo/exercises/find/exercise.js:37:5)
    at next (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:260:17)
    at Exercise.end (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:266:5)
    at Workshopper.end (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:191:12)
    at Workshopper.done (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:323:19)
    at Exercise.<anonymous> (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:149:14)
    at /usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:136:16
    at Exercise.<anonymous> (/usr/local/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/filecheck.js:10:14)
    at FSReqWrap.oncomplete (fs.js:82:15)

from learnyoumongo.

savokiss avatar savokiss commented on August 27, 2024

Hi all, I have the same problem and stucked in exercise 3.But then i realized it wasn't my fault.So I go to exercise 1 and do it again, then exercise 2 and then exercise 3 and it works!
May be it has some connections between them, and you have to run them continuously!

Note that --nojournal and db.close() are also important~

from learnyoumongo.

URobert avatar URobert commented on August 27, 2024

The solution is already provided above by @nalexiou . However, for those running into:
"TypeError: Cannot read property 'collection' of undefined" here are some tips:

1)Make sure in another terminal MongoDb is running (see @Maroo-b 's answer)
2)Make sure you are trying to verify the right file name. I know this is very basic but hey, it happens to all of us every now and then. Uppercase or lowercase in the file name matters ofc (so does the extention).
3)In case you are wondering on what $gt is (and if your new to Mongo like me, you probably are): https://docs.mongodb.org/manual/reference/operator/query/gt/

And lastly another very basic advice: use console logs when stuck, even console.log("Connection succesful"); right after your connection etc. - just remember to wipe them out when you are doing the final check with `learnyoumongo verify" so the actual answer matches the expected outcome.

Hope this helps someone, GL & HF.

from learnyoumongo.

bumbeishvili avatar bumbeishvili commented on August 27, 2024

I was having same problem:
TypeError: Cannot read property 'collection' of undefined

@Maroo-b and @atan77 comments helped me.

so here is steps if anyone having this problem:

  1. ensure you start mongodb on port 27017 with this command from first bash window:
    mongod --port 27017 --dbpath=./data --nojournal

  2. my solution.js looks likes this

var mongo = require("mongodb").MongoClient;


mongo.connect('mongodb://localhost:27017/learnyoumongo',function(err,db){
    if(err) console.log(err);

    var parrots = db.collection('parrots');
    var arg = parseInt(process.argv[2]);

    var result = parrots.find({age:{$gt:arg}});

    result.toArray(function(err,docs){
        if(err) console.log(err);
        console.log(docs);
        db.close();
    });

});
  1. run this command from second bash window to verify solution (without square brackets)
    learnyoumongo verify solution.js

from learnyoumongo.

ygautomo avatar ygautomo commented on August 27, 2024

I have pass the exam 3, with the result.

[ { _id: 5753ba9f84bd376d11a4ebd8, name: 'Jenny', age: 10 } ]

But i've checked on mongo shell using this command

> db
learnyoumongo
> show collections
parrots
system.indexes
> db.parrots.count()
0
> db.system.indexes.count()
1

So actually there is no document in collection parrots?
Where should i check the data?

from learnyoumongo.

evanlucas avatar evanlucas commented on August 27, 2024

The data is removed after the exercise is completed. Thanks!

from learnyoumongo.

kristianjaeger avatar kristianjaeger commented on August 27, 2024

I'm seeing the same issue:
"> show databases
admin (empty)
learnyoumongo (empty)
local 0.078GB
test (empty)"

The entire learnyoumongo db is empty. Is there a way to repopulate this db? Thanks.

from learnyoumongo.

JoeDoyle23 avatar JoeDoyle23 commented on August 27, 2024

Hi @kristianjaeger,

As mentioned above, the collection only exists when the exercise is being run, and deleted when it finishes.

Longer term, it might make sense to populate the database as a step when we test che connection in the first lesson, that way folks can use normal MongoDB tooling to inspect the database to see what they are working with.

from learnyoumongo.

kristianjaeger avatar kristianjaeger commented on August 27, 2024

@JoeDoyle23 Yes, that would be great. Right now, it's difficult to debug one's code as it stands or inspect the database itself.

from learnyoumongo.

therealcode avatar therealcode commented on August 27, 2024

I haven't been able to get this to work - even using the solution.js provided here.

I can start the server just fine with --nojournal. When I shell into mongo and type:
> db
test
> show collections
(blank)

It seems there aren't any sessions running... I went back and completed exercise 1, verified, and 2, then verifired just fine. I am not able to get past 3 for the life of me...

*** I also tried using npm install mongod in the working directory ***

from learnyoumongo.

SQLMD avatar SQLMD commented on August 27, 2024

Thank You! I've been going crazy trying to figure this out..

from learnyoumongo.

NinemillaKA avatar NinemillaKA commented on August 27, 2024

when i run: learnyoumongo verify find.js
i get this
/home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:814
throw err;
^

TypeError: db.collection is not a function
at mongo.connect (/home/ubuntu/workspace/find.js:8:32)
at args.push (/home/ubuntu/workspace/node_modules/mongodb/lib/utils.js:404:72)
at /home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:271:5
at connectCallback (/home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:950:5)
at /home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:811:11
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)

Your submission results compared to the expected:

             ACTUAL                                 EXPECTED                

────────────────────────────────────────────────────────────────────────────────

"" != "[ { _id: 5a846f7fcbad8a21a813b260, name: 'Jenny', age: 10 } ]"
!= ""

────────────────────────────────────────────────────────────────────────────────

✗ Submission results did not match expected!

FAIL

Your solution to FIND didn't pass. Try again!

Someone can help me to solve this? thanks...

from learnyoumongo.

mat-singh avatar mat-singh commented on August 27, 2024

NinemillaKA, did you use the client's db function to associate the database, before getting to the collection? The code is present two comments above....

from learnyoumongo.

Tailsxky avatar Tailsxky commented on August 27, 2024

@midori0507

Solved my issue, thanks!

from learnyoumongo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.