I am new to nodeJs. I am developing a platform where users can subscribe in trial or pro version. I would like to know if I can use the setTimeout method to delete a user's info from db after the subscription expiration date. if this is not possible is there a way to do it or a library that allows you to manage subscriptions?
4 Answers
You can, but it would not be a good approach since you be manging that on memory and if your server restarts you will lose this subscription status.
Why don't you just save on database the subscription date and on user login verify if the subscription date difference from the now is greater than the period of free subscription?

- 527
- 5
- 15
-
Yes. Thank. i don't think about it. I'll try this approch. Thank – AGOSSOU Yannick Mar 03 '20 at 14:25
setTimeout is not reliable, service can go up or down and your request should be persistent.
You need an offline job manager, DB-based, like agenda or similar.
Question is also discussed widely here: I need a Nodejs scheduler that allows for tasks at different intervals

- 1,581
- 1
- 10
- 11
A solution to your problem is to run a CRON
job. That may run once every 24 hours to check user's subscription's and if trial has expired then delete the user. You can use the cron npm package to achieve this.

- 4,608
- 4
- 15
- 41