Zengetsu
Zengetsu

Reputation: 109

Where does the "err" argument come from in Mongoose?

Not sure whether this is more a general JS question than Mongoose per se, but what is the function that provides the "err" argument in the code below?

//Save a new cat called "Thomas" to the "Cats" collection

Thomas.save( function(err, cat) {
    if (err){
        console.log("Cat not saved")
    } else {
        console.log("Saved")
    }
})

Upvotes: 1

Views: 352

Answers (1)

kgangadhar
kgangadhar

Reputation: 5088

What ever Asynchronous operation u are performing save() or findByName() ..etc, when u use callback, Traditionally the first parameter of the callback is the error value. If the function hits an error, then they typically call the callback with the first parameter being an Error object.

If it cleanly exits, then they will call the callback with the first parameter being null and the rest being the return value(s).

asyncOperation ( params.., function ( err, returnValues.. ) {
   //This code gets run after the async operation gets run
});

In your case .save() gives err if hits an error.

Upvotes: 1

Related Questions