Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What parameters are passed to Mongoose callbacks

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])

like image 266
Startec Avatar asked Apr 10 '15 09:04

Startec


People also ask

What is a callback function in Mongoose?

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.

What does Mongoose find return?

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.

Does Mongoose find return an array?

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.

Does Mongoose connect return a promise?

You don't handle a Promise with a callback: mongoose call you're callback if provided, otherwise it return the Promise.


1 Answers

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: For findOne() 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.

like image 172
Jason Cust Avatar answered Sep 27 '22 22:09

Jason Cust