After creation hook

Viewed 60

Let's say I have a mongoose model m.

This model m was created with a Schema s that adds a method to log things:

s.methods.log = function(s) {
  this.theLogger(s);
}

theLogger must be feed at any time, so I feed theLogger at postinit hook.

This works:

const x = await m.findOne({_id: ...});
x.log("something");

The problem here is that this won't work:

const x = new m();
x.log("something"); // <--- theLogger is not defined. 

Is there any way to hook the moment x is created using new operator?

1 Answers
Related