对于MEAN堆栈,我正在学习Mongoose的save()函数,它需要callback。 其API指出 :

Model#save([options], [fn]) Saves this document. Parameters: [options] <Object> options set `` to override [schema's safe option]( [fn] <Function> optional callback 

如何知道可选callback中的参数? API只是举一个例子:

 product.sold =; (err, product, numAffected) { if (err) .. }) The callback will receive three parameters err if an error occurred product which is the saved product numAffected will be 1 when the document was successfully persisted to MongoDB, otherwise 0. 


 [fn] <Function> optional callback with this structure: function(err, theDocumentToBeSaved, [isSaveSuccessful]) 

它可以像下面一样使用。 请注意,第二个参数(文档)必须是调用保存的文档。 (让我知道如果不是这样的话), documentFoo, [isSaveSuccessful]){ if(err){ return next(err); } if (isSaveSuccessful === 1){ // documentFoo has been saved correctly // do stuff with the saved documentFoo } } 



网上收集的解决方案 "Mongoose的保存callback是如何工作的?"