1

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")
    }
})
Zengetsu
  • 109
  • 1
  • 8
  • 1
    Probably the `.save` function? – Jonas Wilms Oct 22 '17 at 09:18
  • You'd need to go poking into the Mongoose source code to find it. (It also **doesn't matter** unless you are trying to edit the Mongoose library itself. The documentation tells you what the function you've written should expect to receive as arguments). – Quentin Oct 22 '17 at 09:20

1 Answers1

1

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.

kgangadhar
  • 4,886
  • 5
  • 36
  • 54