将新事件保存到数据库

时间:2018-08-04 15:06:35

标签: mongodb mongoose

我想创建一个newEvent并将其添加到事件数组中。我该如何完成?我正在使用Mongoose来连接我的数据库

事件API

const newEvent = new Event({
      title: req.body.title,
      start: req.body.start,
      end: req.body.end,
      allday: req.body.allday,
      color: req.body.color,
      textcolor: req.body.textcolor,
      user: req.user.id
    });

事件架构

  user: {
    type: Schema.Types.ObjectId,
    ref: "users"
  },
  date: {
    type: Date,
    default: Date.now
  },
  events: [
    {
      title: { type: String, required: true },
      start: { type: String, required: true },
      end: { type: String, required: true },
      allday: { type: Boolean },
      color: { type: String, required: true },
      textcolor: { type: String }
    }
  ]

1 个答案:

答案 0 :(得分:0)

 const newEvent = new Event({
      events: [
        {
          title: req.body.title,
          start: req.body.start,
          end: req.body.end,
          allday: req.body.allday,
          color: req.body.color,
          textcolor: req.body.textcolor,
          user: req.user.id
        }
      ]
    });
    newEvent.save().then(event => res.json(event));
  }
);