简体   繁体   English

当文档即将过期时通知用户的最佳方式

[英]The best way to notify user when a document is close to be expired

i have a calendar app, i create appointments and i performe an auto delete for them after 5 minutes, i am trying to get all appointments that are 1 minute away from expiring and send a notification to my front-end saying that these appointments are close to be expired.我有一个日历应用程序,我创建约会并在 5 分钟后为它们执行自动删除,我试图让所有距离过期还有 1 分钟的约会,并向我的前端发送通知说这些约会已关闭将过期。

i made an example but i don't get any results:我做了一个例子,但我没有得到任何结果:

async function sendNotification(req: Request, res: Response) {
  const DATE = new Date();
  const NEXT_MINUTE = new Date(DATE.setMinutes(DATE.getMinutes() + 1));
  

  const appointments = await Calendar.aggregate([
    { $match: { expireAt: { $lt: NEXT_MINUTE } } },
  ]);
  console.log("LAST_MINUTE >>", NEXT_MINUTE);

  res.status(200).json(appointments);
}

my question is how to get appointments that will expire in 1 minute?我的问题是如何获得将在 1 分钟内到期的约会?

This is probably due to this note about aggregations in Mongoose...这可能是由于有关 Mongoose 中的聚合的注释...

unlike query filters, Mongoose also doesn't cast aggregation pipelines.与查询过滤器不同,Mongoose 也不强制转换聚合管道。 That means you're responsible for ensuring the values you pass in to an aggregation pipeline have the correct type.这意味着您有责任确保传入聚合管道的值具有正确的类型。

There's probably an issue with passing in a Date instance.传入Date实例可能存在问题。

You don't appear to actually need an aggregate.您似乎实际上并不需要聚合。 Try using a normal query instead尝试改用普通查询

const NEXT_MINUTE = new Date(Date.now() + 60000);

const appointments = await Calendar.find({
  expireAt: { $lt: NEXT_MINUTE }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM