model: ***(i changed start_time to type: Date and i added email_sent)
start_time: {
type: Date,
required: true,
},
email_sent: {
type:Boolean,
default:"false",
required:true
},
hour: {
type: String,
required: true,
},
courtId: {
type: String,
required: true,
},
userId: {
type: ObjectId,
ref: 'userModel',
required: true,
},
});
Server:
cron.schedule('00 * * * * *', () => {
Modal.find({}).
then(allUsers => {
for(let user of allUsers){
if(Date.now() < (user.start_time - (24*60*60*1000) * 7) && user.email_sent === false){
//sendEmail();
// here you need to update user.email_sent: true. and save.
}
}
})
});
*Also i will suggest to do, find only email_sent : false. you can check how to do it with mongoose. (then you dont need to check it in the if statement)
*Model.find({ email_sent: "false" })
i just did it out of my mind, i didnt check, errors you can face:
- you will need to convert the start_time
- check the boolean email_sent how you get it (as a string or boolen) it will affect the if statement.
this is just the idea how to achieve it. i didnt test the code.
Please try the code and let me know if you face any issues
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…