evanlucas / learnyoumongo

MongoDB workshop.
MIT License
306 stars 89 forks source link

ERROR IN AGGREGATE EXERCISE #30

Open mojoboss opened 8 years ago

mojoboss commented 8 years ago

This is my solution for the aggregate exercise, the problem is its giving this error-

var size = process.argv[2]; var url = "mongodb://localhost:27017/learnyoumongo"; var mongo = require("mongodb").MongoClient; mongo.connect(url, function(err, db){ if(err) throw err; var collection = db.collection("prices"); collection.aggregate({$match: {"size": size}}, {$group: {_id: "average"}, average:{$avg: "$price"}}).toArray(function(err, results){ if(err) throw err; console.log(Number(results[0].average).toFixed(2)); db.close(); }); });

error-

/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/exercises/aggregate/exercise.js:34 db.collection('prices').remove({}, function(err) { ^ TypeError: Cannot read property 'collection' of undefined at Exercise. (/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/exercises/aggregate/exercise.js:34: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. (/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 Exercise. (/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/filecheck.js:10:14) at FSReqWrap.oncomplete (fs.js:82:15)

I'm not even using .remove(), but it still gives the same error. I used the official solution but still getting the same error.

eliotn commented 8 years ago

I would like to say that I got the exact same error just now. Is the database being initialized properly?

Atomk commented 7 years ago

Note: I understand this is an old issue, people still use this workshopper though, so...

@mojoboss This is not a bug, there is a lot going on in this lesson and it's easy to miss something, your solution has a couple of issues.

First, your $group object has a weird structure, you wrote (I formatted it to make it easier to see the difference):

{
  $group: {_id: "average"},
  average: {$avg: "$price"}
}

but it should be:

{
  $group: {
    _id: "average",
    average: { $avg: "$price" }
  }
}

Second, the parameter of the aggregate function should be an array of objects, while you passed two objects. What you did:

collection.aggregate(
  { $match: { ... }},
  { $group: { ... }}
)

The correct way (the objects are now elements of an array, notice the square brackets):

collection.aggregate([
  { $match: { ... }},
  { $group: { ... }}
])

There you have it! After these fixes everything works correctly on my machine. @evanlucas I think you can close this issue.