使用新日期重复mongo文档

时间:2016-03-01 18:06:02

标签: javascript mongodb mongoose

我正在使用Mongo DB在React中为客户创建一个事件日历。在面向公众的一方,用户将能够查看事件,并将其添加到他我们工作的手机上的日历中。我遇到的问题是事件很少每周都会发生变化,如果它们可能是教师的名字,或者房间的变化。因此,在管理员方面,当管理员创建事件时,他们将能够点击重复事件直到“X”日期。在服务器上,我需要重新创建文档,直到事件的开始时间或结束时间等于重复日期的日期。

我的事件架构如下所示:

var activitySchema = mongoose.Schema({
    "room": String,
    "type": String,
    "title" : String,
    "start": {type: Date},
    "end": {type: Date},
    "instructor" : String,
    "desc": String,
    "image": String,
    "style": {
        "backgroundColor": String
    }
})

当用户点击创建表单上的提交时,这就是火灾:

onSubmit() {
    var startTime = {start: this.state.start || this.state.data};
    var endTime = {end: this.state.end || this.state.data};
    var color = {style: {backgroundColor: this.state.color || this.state.data}}
    var image ={image: this.state.image || this.state.data};

    var myForm = this.refs.MyForm.getValue();
    var payload = _.merge(myForm, startTime, endTime, color, image);
    if (payload._id) {
        superagent.put("http://"+this.context.config.API_SERVER+"/api/v1.0/activity/").send(payload).end((err, res) => {
            err ? console.log(err) : console.log(res);
        });
    } else {
        delete payload._id; //remove the empty id object from the post since it's blank.
        superagent.post("http://"+this.context.config.API_SERVER+"/api/v1.0/activity/").send(payload).end((err, res) => {
            err ? console.log(err) : console.log(res);
            this.refs.MyForm.reset();
        });
    }
},

所以我需要发生的是当他们点击提交时,它会一遍又一遍地重复该文件,但是日期增加了7天,所以它在一周的同一天每周重复一次。

现在,它只是为所选日期和时间一次创建一个事件,并在日历上显示。我正在使用MomentJS进行时间处理,我将尝试使用Moment-Recur,但安装时出现错误,但它似乎是一个尚未解决的常见问题。

任何帮助或建议都会很棒,我不是一个后端人,并且不知道如何做到这一点。

2 个答案:

答案 0 :(得分:0)

我们有非常相似的要求,最终使用了这个npm包:

https://github.com/tritech/node-icalendar

这允许管理真实的iCal对象,这些对象可以被典型的日历应用程序识别,例如Outlook,Google Calendar等。是支持重复发生的事件,因此您只需要一个具有重复定义的事件对象。 如果您需要重复发生的事件,则会有getOccurences()函数。不幸的是,文档很少,单元测试是最好的起点。

您可能不得不重新考虑事件的数据模型 - 在iCal,Outlook的世界中......反复出现的事件只是一个事件,而不是多个事件。

在客户端(网络应用),我们使用此服务:https://www.addevent.com/以允许用户将定期事件添加到他的日历中。

答案 1 :(得分:0)

所以我最终解决了我的问题。虽然这可能不是重复事件的最佳方式,但我所知道的是让我周围的多个人做事。

if(this.refs.repeatCheckbox.checked){
      //let endRepeat = moment().endOf("month").format();
      let endRepeat = moment(this.state.repeat).add({days: 1}).format();
      let start = moment(this.state.start).format();
      let end = moment(this.state.end).format();

      do {
        let newStart = {start: start};
        let newEnd = {end: end};

        var myForm = this.refs.MyForm.getValue();
        var payload = _.merge(myForm, newStart, newEnd, color, image);

        if (payload._id) {
            superagent.put("http://"+this.context.config.API_SERVER+"/api/v1.0/activity/").send(payload).end((err, res) => {
                err ? console.log(err) : console.log(res);
            });
        } else {
            delete payload._id; //remove the empty id object from the post since it's blank.
            superagent.post("http://"+this.context.config.API_SERVER+"/api/v1.0/activity/").send(payload).end((err, res) => {
                err ? console.log(err) : console.log(res);
                this.refs.MyForm.reset();
            });
        }

        start = moment(start).add({days: 7}).format();
        end = moment(end).add({days: 7}).format();
      } while (end < endRepeat);
    } else {
      var myForm = this.refs.MyForm.getValue();
      var payload = _.merge(myForm, startTime, endTime, color, image);

      if (payload._id) {
          superagent.put("http://"+this.context.config.API_SERVER+"/api/v1.0/activity/").send(payload).end((err, res) => {
              err ? console.log(err) : console.log(res);
          });
      } else {
          delete payload._id; //remove the empty id object from the post since it's blank.
          superagent.post("http://"+this.context.config.API_SERVER+"/api/v1.0/activity/").send(payload).end((err, res) => {
              err ? console.log(err) : console.log(res);
              this.refs.MyForm.reset();
          });
      }
    };

因此,如果用户选中了重复框,并输入了他们希望重复的日期,它将循环提交功能并添加7天,直到结束日期等于所选的重复日期。如果他们不检查框并选择日期,它将只创建一个事件。可能不是最好的解决方案,但它可以工作,它适用于我正在使用的日历。

相关问题