FAQ: Mongoose Fundamentals - Methods

This community-built FAQ covers the “Methods” exercise from the lesson “Mongoose Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Testing for Web Development

FAQs on the exercise Methods

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I’m wondering about our statics example code:

poemSchema.statics.firstAlphabetically = function(callback) {
  return this.findOne({}).sort('title').exec(callback);
}

I would expect this.findOne({}) to return a single instance of a model object. But we wouldn’t be able to then call sort if that were the case. What does this.findOne({}) return? I tried logging its return value to the console and got a wall of text.

2 Likes

I have the same question. Mongoose documentation doesn’t really help… If it were .find() instead of .findOne({}), it would make sense, though.

What I learned from the Mongoose documentation is that the .find() (without any argument) will return a Query object (which I assume is a set of documents). (See the documentation on Query().)

And the Query object has a method .sort() that sorts the documents in the Query object.

But why .findOne({}) to get the first document in the sorted order? I have no idea…

I can pass the second checkpoint for this exercise, but when I enter node exercise.js in the terminal, I get this error:

$ node exercise.js
connecting to database...

dropping old data...

running function...

Found one: cloak
Found 3 magic objects
(node:83) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: MagicItem.findMostExpensive is not a function
(node:83) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

This is my code which the exercise marks as correct:

const {mongoose, runWithDatabase} = require('./database');
const manyItems = require('./items');

const magicItemSchema = new mongoose.Schema({
  item: {
    type: String,
    required: true
  },
  magicalProperty: {
    type: String ,
    required: true
  },
  unitCost: {
    type: Number,
    required: true
  },
  totalUnits: {
    type: Number,
    required: true
  }

})

const MagicItem = mongoose.model('MagicItem', magicItemSchema);

magicItemSchema.statics.findMostExpensive = 
  function(callback) {
  return this.findOne({}).sort('unitCost').exec(callback);
}

magicItemSchema.methods.use = function(callback) {
  this.totalUnits -= this.unitCost;
return this.save();
}

runWithDatabase(async () => {
	await  MagicItem.create(manyItems);

	let finder = await MagicItem.findOne({ item: 'cloak' });
    console.log(`Found one: ${finder.item}`);
    
    let cheapObjects = await MagicItem.find({ unitCost: { $lt: 50 }})
    console.log(`Found ${MagicItem.length} magic objects`);

    const mostExpensive = await MagicItem.findMostExpensive();
console.log(`The most expensive object is the ${mostExpensive.item}`);
console.log(`The ${mostExpensive.item} started with ${mostExpensive.totalUnits} charges.`);
console.log(`Using ${mostExpensive.item}...`);    
await mostExpensive.use();
console.log(`The ${mostExpensive.item} has ${mostExpensive.totalUnits} charges left.`);

});



The method is supposed to return only the first alphabetical by title. If you used find, it would return all titles in alphabetical order. You only wanted the 1st one.

I think it simply has to do with your indentation, your error code specifies:

TypeError: MagicItem.findMostExpensive is not a function

So I went to look there, and it seems your code is as good as mine, except that it may be badly indented. It should look like this:

magicItemSchema.statics.findMostExpensive = function(callback) {
  return this.findOne({}).sort('unitCost').exec(callback);
}

I believe it sorts the model objects and only after does it return the “one” top most instance, after the sorting process has finished executing.

As mentioned in the exercise:

The method is part of the model, which in this example would return the first document, AFTER sorting the values of the title path alphabetically.