vivid
vivid

Reputation: 1145

setter get mongoose object id before save

Is it possible in mongoose to get _id before save in other property setters?

Example i got a field "fileName" and want to make function for setter that will pass fieldName and objectId to other function for processing.

Upvotes: 0

Views: 2276

Answers (1)

Dylan Lott
Dylan Lott

Reputation: 421

To expand on my earlier commment, you could do something like this:

schema.pre('save', function(next) {
  // do stuff
  next();
});

Note: .pre('save') hooks don't run on updates such as update() or findOneAndUpdate()

There is also a .post('save', function() {}) hook that you can use as well.

Upvotes: 1

Related Questions