The three-step find-edit-save approach
This approach requires a few more steps and a bit more code than the helper methods but is more fully featured. The save()
command accepts only one parameter, a callback function to run once the save has completed. This callback function can pass an error or return the saved object. Look at the following example:
// 1: FIND the record User.findOne( {email : 'simon@theholmesoffice.com'}, function(err, user) { if(!err){ // 2: EDIT the record user.name = "Simon"; // 3: SAVE the record user.save(function(err,user){ console.log('User saved:', user); }); } }; );
As you can see there is a bit more code required, and there's also an extra callback level. However, the code remains pretty simple to follow. To make it even easier, you can also define the callbacks as separate functions. We'll see this later.