wclr / mongoose-fill

Virtual async fileds for mongoose.js
61 stars 8 forks source link

Mongoose hooks #17

Open armand1m opened 7 years ago

armand1m commented 7 years ago

Hey there, thanks for contributing for open source. I was using in your package for integrating some firebase data into something I'd need, and I wonder if it is possible to use this inside of schema.pre('find', callback) callbacks for example. Something like this:

schema.fill('user', function(callback) {
  if (this.userFirebaseUid) {
    return FirebaseAdmin
      .auth()
      .getUser(this.userFirebaseUid)
      .then(user => {
        callback(null, user.providerData[0])
      })
      .catch(err => {
        callback(err, null);
      })
  }

  callback();
});

const autoPopulateRefs = function autoPopulateRefs(next) {
  this.fill('user');
  this.populate('source');
  this.populate('category');

  next();
}

schema
  .pre('findOne', autoPopulateRefs)
  .pre('find', autoPopulateRefs);
wclr commented 7 years ago

I wonder if it is possible to use this inside of schema.pre('find', callback) callbacks

I don't know, does it work? =)

armand1m commented 7 years ago

@whitecolor no, it doesn't :(