We have a recurring pattern in our application when persisting an instance of a mongoose model to the data, which looks like this:
var newClient = new Client.model( data );
Q.ninvoke( newClient, "save" )
.then( function( newEntity, numberAffected ) {
numberAffected = newEntity[ 1 ];
newEntity = newEntity[ 0 ];
} );
Usually, save
would accept a callback which takes 3 arguments. The first error argument is stripped off by Q and optionally handled in a fail
callback.
The remaining parameters are passed to the then
callback as an array. We now break that array apart to give meaning to the entries, but this seems a little inconvenient.
Is there a better way?