0

This is the stack trace my application is providing:

Error: Arguments must be aggregate pipeline operators
at Aggregate.append (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/mongoose/lib/aggregate.js:89:11)
at new Aggregate (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/mongoose/lib/aggregate.js:48:17)
at Function.aggregate (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/mongoose/lib/model.js:2415:17)
at Function.storeSchema.statics.getTopStores (/Users/flywheel/zprojects/whatsgood/the-ashevillian/models/Store.js:82:15)
at exports.getTopStores (/Users/flywheel/zprojects/whatsgood/the-ashevillian/controllers/storeController.js:198:30)
at /Users/flywheel/zprojects/whatsgood/the-ashevillian/handlers/errorHandlers.js:11:12
at Layer.handle [as handle_request] (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/layer.js:95:5)
at /Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:335:12)
at next (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:174:3)
at router (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:47:12)

The relevant code- these are the only two places aggregate is called in the entire application:

Store.js

storeSchema.statics.getTagsList = function() {
  return this.aggregate([
    { $unwind: '$tags' },
    { $group: { _id: '$tags', count : { $sum: 1 } }},
    { $sort: { count: -1} }
  ]);
};

storeSchema.statics.getTopStores = function (){
  return this.aggregate([
      // Look up stores and populate reviews
    { $lookup: { from: 'reviews', localField: '_id',
      foreignField: 'store', as: 'reviews'}},
      // Filter for stores with 2 or more reviews
    { $match: { 'reviews.1': { $exists: true } }},
      // Add average reviews field
      // TODO: Changed with $addfield in mongodb 3.4?
      // UPDATE
    { $project: {
        photo: '$$ROOT.photo',
        name: '$$ROOT.name',
        reviews: '$$ROOT.reviews',
        slug: '$$ROOT.slug',
        averageRating: { $avg: '$reviews.rating' }
      } },
    // sort it by our new field, highest reviews first
    { $sort: { averageRating: -1 }},
    // limit to at most 10
    { $limit: 10 }
  ])
};

My attempts at finding solutions to this problem have so far led me to some examples that were different or unclear, or to the docs which don't address the error message. Surely what is going on is there is some aggregate pipeline operator that should be used in these two functions in the code block but I don't know what.

You can deep dive into the repository at https://github.com/airbr/whatsgood if you wish.

morganwebdev
  • 170
  • 11

1 Answers1

0

The solution was to upgrade Mongoose to version 5.0.

Furthermore, I had to add url parser mentions like in this answer:

Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true

morganwebdev
  • 170
  • 11