MongoDB and Mongoose - Chain Search Query Helpers to Narrow Search Results - Array.Sort problem

Tell us what’s happening:

I wanted to solve it in another way, using await keyword.

const queryChain = async (done) => {
  const foodToSearch = "burrito";
  var chain = new mongoose.Query();
  chain = await Person.find({favoriteFoods : foodToSearch});
  chain = await chain.sort({name: 1}).limit(2).select({
    name: 1, age: 0 , favoriteFoods: 1 
  });
  chain.exec((err,data)=>{
    if (err) {return console.log(err)};
    done(null ,data);
  }); 
};

I’m getting the error:
TypeError: The comparison function must be either a function or undefined
at Array.sort ()

It seems that chain.sort() is interpreted as standard JS Array.sort(), not Query.prototype.sort()
Although I explicitly declared chain as an instance of mongoose.Query();

How to fix that error?

###Your project link(s)

solution: boilerplate-mongomongoose - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0

Challenge Information:

MongoDB and Mongoose - Chain Search Query Helpers to Narrow Search Results

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.