These are chat archives for Automattic/mongoose

21st
Jun 2017
Mihail
@metronom72
Jun 21 2017 17:56

@gnwankpa

Well, if you will use promises, you should redefine mongoose promises, something like this mongoose.Promise = global.Promise
And you can use like this

YourMode.find({
  ....
}).then( docs => {
  /** something cool thins */
}).catch( err => {
  /** handle error state*/
})

You can use callbacks , if you prefer speed and want not handled begaviour

const fn = ( ...params ) => {
  let result = false;
  YourModel.find(yourObjectQuery, (err, docs) => {
    if ( err ) {
      // handle error state
    }
    // do something with documents
    result = documents;
  })

  if ( result) {
    return result;
  }
}

or combine them

const fn = ( ...params ) => {
  return new Promise((resolve, reject) => {
    YourModel.find(query, (err, docs) => {
      if ( err ) {
        // handle error
        reject( err );
      }
      // do something cool
      resolve ( docs );
    });
  });
};