简体   繁体   中英

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") } })

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM