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.
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:
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.