javascript - Mongoose Post Hook for create() rather than update() - Stack Overflow

Maybe I'm misunderstanding, but everything I can find for having a post hook for create() on a mon

Maybe I'm misunderstanding, but everything I can find for having a post hook for create() on a mongoose model brings up the update() method instead. Are create() and update() the same?

What I want to do is when a User document is created, send a wele email, without having to manually call the method on every route/controller that creates a user.

I understand a little about pre- and post- hooks, and I have a pre-remove hook:

userSchema.pre('remove', async function() {
    for (let response of this.responses) {
        Response.findByIdAndRemove(response);
    };
});

But I can't find anything within mongoose docs for a post-hook for create().

If create() and update() are the same, what stops this wele email from being sent any time the user's information is changed? I only want this to send once, at the very beginning.

Let me know if I'm clear as mud

Maybe I'm misunderstanding, but everything I can find for having a post hook for create() on a mongoose model brings up the update() method instead. Are create() and update() the same?

What I want to do is when a User document is created, send a wele email, without having to manually call the method on every route/controller that creates a user.

I understand a little about pre- and post- hooks, and I have a pre-remove hook:

userSchema.pre('remove', async function() {
    for (let response of this.responses) {
        Response.findByIdAndRemove(response);
    };
});

But I can't find anything within mongoose docs for a post-hook for create().

If create() and update() are the same, what stops this wele email from being sent any time the user's information is changed? I only want this to send once, at the very beginning.

Let me know if I'm clear as mud

Share Improve this question edited Apr 15, 2020 at 21:16 R Greenstreet asked Apr 9, 2020 at 15:44 R GreenstreetR Greenstreet 7407 silver badges25 bronze badges 1
  • For future reference, if yuo look at their docs under pre mongoosejs./docs/api.html#schema_Schema-pre you will notice they actually show an example of detecting create via the date fields created the by the timestamp option, while not a direct method of asking if created it does work for most models for me – Sammaye Commented Dec 30, 2021 at 10:23
Add a ment  | 

1 Answer 1

Reset to default 7

I found the answer, finally, in the Mongoose docs in a roundabout way through a github feature request: schema.queue: http://mongoosejs./docs/api.html#schema_Schema-queue

So I define the method(s) that I want to execute at the time the document is instantiated, then just use the schema.queue mand like so:

schema.queue('methodName',[args]);

For the time being, I left the args array empty because they're only operating on themselves, so no other information needs to go in.

The docs don't say it specifically, but I would assume since declaring methods looks to be the same as declaring any function, the queue method can be called before or after declaring the method it calls, but I played it safe and put it after.

Man, this is exciting.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745048424a4608236.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信