Open cjknowles opened 9 years ago
The parrots collection is created by the exercise
I realized that the HINTS for the FIND problem could have been better. Went ahead and added additional info in 9def6d50f5e91b76123307d61a59efce6efc56d5.
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?
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
Also, how did you run the solution?
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.
Yea, there won't be any content because it gets cleaned up after the exercise is done running.
try using learnyoumongo verify file
to verify the solution instead
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.
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?
I am verifying the solution like a good cheat...
~/.npm/learnyoumongo/1.1.4/package/exercises/find/solution/solution.js
@mr-cjk did you run "npm install mongodb"? other thing are you can paste the code in a gist.
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.
@EightArmCode can you give me some more details on the issues you are having?
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
Can you share the code you ran to get that error?
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()
})
})
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!!!
It seems it's necessary to run "npm install mongodb" before doing exercise 3.
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
> kerberos@0.0.14 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
mongodb@2.0.43 node_modules/mongodb
├── readable-stream@1.0.31 (isarray@0.0.1, string_decoder@0.10.31, inherits@2.0.1, core-util-is@1.0.1)
├── es6-promise@2.1.1
└── mongodb-core@1.2.12 (bson@0.4.12, kerberos@0.0.14)
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...
I have same problem
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)
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).
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.
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.
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.
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~
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.
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();
});
});
3) run this command from second bash window to verify solution (without square brackets)
learnyoumongo verify solution.js
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?
The data is removed after the exercise is completed. Thanks!
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.
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.
@JoeDoyle23 Yes, that would be great. Right now, it's difficult to debug one's code as it stands or inspect the database itself.
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
Hey guys, the issue happen with latest version of mongodb, try to use another version, remove node_modules and
npm install mongodb@2.2.33
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: https://github.com/evanlucas/learnyoumongo/issues/64
Thank You! I've been going crazy trying to figure this out..
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!
Your solution to FIND didn't pass. Try again!
Someone can help me to solve this? thanks...
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....
@midori0507
Solved my issue, thanks!
Hey --
I just tried this tutorial and hit a wall on exercise three, as I do not have a parrots collection?
I can see this is a fresh repo, sorry if I've jumped the gun!