node.js - 如何在 Node.js 和 Mongoose 中在一段时间后更新用户?

标签 node.js mongodb express mongoose

如何根据用户的有效服务续订日期自动从用户的可用积分中扣除积分?

让我描述一下当前的情况。目前,我正在开发一个 Express 应用程序,它基本上是一个计费系统,它将自动管理客户并根据他的事件服务向他们收费。但我卡住了!如果用户(用户)余额不足,如何自动从用户帐户中扣除积分并续订服务或暂停服务。

这是我所拥有的。

服务架构:

var mongoose= require('mongoose');
var Schema = new mongoose.Schema({
  user:{
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
    required: true
  },
  services:{
    username: {type: String, required: true, unique: true},
    package:  {type: String, required: true},
    domain:   {type: String, required: true, unique: true},
    status: String,
    dateAdded: {type: Date, default: Date.now },
    lastRenewed: Date,
    nextRenewal: Date,
    renewalCost: Number // Renewal Cost Of The Service.
  }
})

module.exports  = mongoose.model('Service', Schema);

用户架构:

var mongoose= require('mongoose');

var Schema = new mongoose.Schema({
  name:{
    type: String,
    min: 1,
    required: true
  },
  username:{
        type: String,
        unique: true
  },
  password:{
    type: String, // Storing Hashed Password
    required: true
  },
  salt:{
    type: String
  },
  meta: {
    email: String,
    phone: Number,
    dateOfBarth: String,
    address: {
      companyName: String,
      address1: String,
      address2: String,
      city: String,
      state: String
   }
 },

 credit:{type: Number, default: 0},  // Avilable Credits
 isAdmin: {type: Boolean, default: false} // If The User Is Admin Or Not?


});

module.exports  = mongoose.model('User', Schema);

现在我想在“nextRenewal”日期运行查询或函数(或类似的东西),并从用户的可用积分中扣除续订费用,保存用户并延长下一个续订日期。

Answer: Use npmjs.com/package/node-schedule To schedule the job.

Thanks to @Kilizo

最佳答案

有两种主要方法可以做到这一点:

  • 使用cron作业
  • 使用Windows调度程序

Windows 调度程序:您可以创建一个具有所需功能的项目,并将该项目添加为 Windows 调度程序中的任务,并根据您所需的日期安排该任务。

cron:您可以使用 cron 作业根据您所需的日期安排任务。

我建议为此编写一个 cron,因为它在 Web 服务器上运行,并且将一直工作到您的服务器停止为止,而 Windows 调度程序在您的本地计算机上运行,​​并且仅在您的计算机正在运行时才工作。

关于node.js - 如何在 Node.js 和 Mongoose 中在一段时间后更新用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44720591/

相关文章:

javascript - 在 Windows 上使用meteor.js 和 mongoDB

node.js - 查找数组中某个位置的元素的最大值

node.js - Node js : getting error "listener must be a function"

javascript - 在 Node JS 中实现 Bluebird 嵌套循环 Promise

ruby - 如何处理 mongodb 在 ruby​​ 中的 E11000 重复键错误

javascript - 与其他 js 文件共享 MongoDB 连接

javascript - 路由和路由处理程序之间的函数未按预期执行

node.js - 按 id 过滤 KeystoneJS 关系

node.js - Nodejs 由于某种原因找不到 ffmpeg 模块

node.js - 如何在redis中持久化HTTP响应