Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR IN AGGREGATE EXERCISE #30

Open
mojoboss opened this issue Feb 16, 2016 · 2 comments
Open

ERROR IN AGGREGATE EXERCISE #30

mojoboss opened this issue Feb 16, 2016 · 2 comments

Comments

@mojoboss
Copy link

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
Copy link

eliotn commented Oct 2, 2016

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

@Atomk
Copy link

Atomk commented Sep 7, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants