In the mongoose documentation it frequently lists an optional callback for certain query operators (like findOneAndUpdate
), however, it does not mention what parameters (arguments) the callback takes. What are they, and how would I find out?
Also, if conditions
, update
etc. are optional, and I want to specify a callback at the end, do I have to pass in the null
value, or empty objects or can I just specify the callback -- and hose does Mongoose know?
Model.findOneAndUpdate([conditions], [update], [options], [callback])
All callbacks in Mongoose use the pattern: callback(error, result) . If an error occurs executing the query, the error parameter will contain an error document, and result will be null. If the query is successful, the error parameter will be null, and the result will be populated with the results of the query.
find() function returns an instance of Mongoose's Query class. The Query class represents a raw CRUD operation that you may send to MongoDB. It provides a chainable interface for building up more sophisticated queries. You don't instantiate a Query directly, Customer.
Return valueThe returned value could be an array of documents, a single document if it matches only one, or an empty array if no document is matched.
You don't handle a Promise with a callback: mongoose call you're callback if provided, otherwise it return the Promise.
For nearly all mongoose queries the provided callback
function will be called with two arguments in the node callback pattern callback(err, results)
as stated in the documentation:
Anywhere a callback is passed to a query in Mongoose, the callback follows the pattern
callback(error, results)
. What results is depends on the operation: ForfindOne()
it is a potentially-null single document,find()
a list of documents,count()
the number of documents,update()
the number of documents affected, etc. The API docs for Models provide more detail on what is passed to the callbacks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With