Mongoose: where does done() function get defined?

Apologies if this is a stupid question, but where does the done() function get defined?

All I can see in myApp.js is calls to the function with arguments like ‘null’, ‘data’ , etc.

Im struggling to understand how the done function actually works…

Your code so far

Your browser information:

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

Challenge: Use model.find() to Search Your Database

Link to the challenge:

The done function is defined by the caller of the function that has done as a parameter.
Let’s look at an example:

const findTeacher = (teacherId, done) => {
    Teacher.findTeacherById(teacherId, (err, data) => {
        if (err) return done(err)
        return done(null, data)

// This is how the function is called, most probably in another file
findTeacher("somerealteacherId", (err, data) => {
    if (err) {

In short, the findTeacher function has a callback function as its second argument. This callback function has the signature of taking two arguments.
When we call findTeacher we need to pass to it a function as its second argument. We create that function on the fly (that’s the done function you we looking for).

Hope that helps.

1 Like

thank you, yes that is very helpful!

its not a stupid question, its quite confusing to understand the mongoose conventions at first while you still dont have a proper grasp of how mongoose logic is built. I still have my troubles completely getting it myself, but ill try to explain what i gathered in the most accessible language.
Since its important to run Mongoose commands in successive order(find a document>modify a document>return the saved document etc.), we want to make sure each command has been completed before we queue the next one, as normally, the next command would require updated data from the previous command. This is where the so called “done()” function comes in place. You can call this function anything, its just a callback function, defined by you, usually representing the next ‘command’ you want to queue after the current comman(function) has been fulfilled. So in order to do that, when we defined a mongoose function, which is meant to execute some command, we pass along the done function(or next function, or whatever you wanna name it) if we want to follow up with another command. No done function is required when the current function is the final mongoose command in our block.
As you can see the done callback generally has 2 parameters, “error” and “data”. Again they are named after conventions, you can put any name you find suitable. Those parameters are there again because of mongoose functuin practices, where we always pass the error as the first parameter and data as the second. If there is an error, we obviously wont get any data and all we are left to do is return the error. If there is no error, we can proceed with the next command in our code and run that function/command by passing the error argument as null and our updated data respectively.

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