0

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?

Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138

1 Answers1

0

We're now using mongoose-q. The same code now looks like this:

var newClient = new Client.model( data );

newClient.saveQ()
  .then( function( newEntity, numberAffected ) {
  } );
Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138