预订提醒 Nodemailer node-cron MongoDB

Booking reminder Nodemailer node-cron MongoDB

我在发送预订提醒电子邮件时遇到问题。我使用 nodemailer 和 node-cron。它运作良好。我想从数据库中读取日期,例如预订前一周发送电子邮件。我不知道该怎么做。我的模型:

  start_time: {
    type: String,
    required: true,
  },
  hour: {
    type: String,
    required: true,
  },
  courtId: {
    type: String,
    required: true,
  },
  userId: {
    type: ObjectId,
    ref: 'userModel',
    required: true,
  },
});

const cron = require('node-cron');
const nodemailer = require('nodemailer');
const { getMaxListeners } = require('../config/database');

const sendEmail = function () {
  const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: `${process.env.EMAIL_ADDRESS}`,
      pass: `${process.env.EMAIL_PASSWORD}`,
    },
  });

  const mailOptions = {
    from: `${process.env.EMAIL_ADDRESS}`,
    to: `${''}`,
    subject: 'Link To Reset Password',
    text:
      'wiadomosć',
  };

  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      console.log(error);
    } else {
      console.log('Email sent: ' + info.response);
    }
  });
};

module.exports.task = function () {
  cron.schedule('00 * * * * *', () => {
    console.log('send');
    //sendEmail();
  });
};

我认为开始时间解析是最新的?如何只读取一周后的这个预订?

型号:***(我将 start_time 更改为键入:日期并添加了 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,
  },
});

服务器:

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.
    }
   }
  })
});

*我也建议这样做,只找到 email_sent : false。你可以检查如何用猫鼬来做。 (那么你不需要在 if 语句中检查它) *Model.find({ email_sent: "假" })

我只是随心所欲地做了,我没有检查,你可能会遇到的错误:

  1. 您需要转换 start_time
  2. 检查布尔值 email_sent 你如何获得它(作为字符串或布尔值)它会影响 if 语句。

这只是如何实现它的想法。我没有测试代码。 请尝试代码,如果您遇到任何问题,请告诉我

我正在将字符串解析为 DATE。 2021-1-16 至 2021-01-15T23:00:00.000Z

const nodemailer = require('nodemailer');
const { getMaxListeners } = require('../config/database');
const reservationModel = require('../models/reservationModel');

const sendEmail = function () {
  const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: `${process.env.EMAIL_ADDRESS}`,
      pass: `${process.env.EMAIL_PASSWORD}`,
    },
  });

  const mailOptions = {
    from: `${process.env.EMAIL_ADDRESS}`,
    to: `${''}`,
    subject: 'Link To Reset Password',
    text: 'wiadomosć',
  };

  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      console.log(error);
    } else {
      console.log('Email sent: ' + info.response);
    }
  });
};

module.exports.task = function () {
  cron.schedule('00 * * * * *', () => {
    console.log('scheduled');
    reservationModel.find({ email_sent: false }).then(allReservation => {
      for (let result of allReservation) {
        let date = new Date(result.start_time);
        console.log(date);
        if (Date.now() < date - 24 * 60 * 60 * 1000 * 7) {
          console.log('send');
          sendEmail();
        }
      }
    });
  });
};

邮件从未发送过 2021-01-15T23:00:00.000Z - 迄今为止的字符串 1610277840044 Date.now()