如何在X时间过后自动更新博客文章? Node.js Express Mongoose

时间:2018-03-15 01:02:23

标签: node.js express mongoose

我想更新博客文章的优先级,如果它超过2-3天,我不知道如何实现这一点。在我的路线中,我尝试确定博客文章是否超过2天,如果是这样,它将改变优先级' high'到了'。到目前为止没有运气,从我在线阅读的内容来看,似乎可能有npm软件包可以为我实现这一目标。

  

Mongoose Schema

var blogSchema = new mongoose.Schema({
  image: {
    type: String,
    trim: true
  },
  priority: {
    type: String,
    default: "",
    trim: true
  },
  title: {
    type: String,
    trim: true
  },
  content: {
    type: String,
    trim: true
  },
  author: {
    id: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      trim: true
    },
    username: {
      type: String,
      trim: true
    },
    name: {
      type: String,
      trim: true
    },
  },
  slug: {
    type: String,
    unique: true,
    trim: true
  },
  status: {
    type: String,
    trim: true
  },
  viewCount: {
    type: Array,
    timestamps: {
      createdAt: 'createdAt',
      updatedAt: 'updatedAt',
      trim: true
  } 
  },
  category: {
    type: String,
    trim: true
  },
  categorySlug: {
    type: String,
    trim: true
  },
  tags: {
    type: String,
    trim: true
  },
  updated: {
    type: Boolean,
    default: false,
  },
  date: { type: Date, default: Date.now , trim: true},
  comments: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Comment',
      trim: true
    },
  ],
},{
  timestamps: {
      createdAt: 'createdAt',
      updatedAt: 'updatedAt'
    }
 });
  

我的路线

router.get('/', function (req, res) {

blog.find({ priority: ['high'] })
.sort({date: -1})
.limit(1)
.exec(function(err, high) {
  if (err || !high) {
    console.log(err);
    req.flash('Troubleshooting Error')
    redirect('/');
  } else {
    if(high && Date.now() > high.createdAt + 86400000  ) {
      var priority = {slug: 'slug', priority: 'high'}
      var newPriority = '';
      blog.findOneAndUpdate(priority, newPriority,function(err, updated){
        if(err) {
          console.log(err);
        } else {
          console.log('Successfully updated priority');
        }
      });
    }
  };
});

0 个答案:

没有答案