如何为 Vue & Express & Mongo / mongoose 的日期类型指定默认值



我正在使用vuejs,expressjs和mongodb。

我有一个截止日期:{type:date,dotional:true}在猫鼬中,但是如果我不向Vue的输入发送截止日期,我会在Express的Create API中获得错误。

如果我不指定日期值,我想生成零。

我该怎么做?

mongoose.js

...
  deadline: {
    type: Date,
    optional: true
  },

utaes.js

  let post = new Post({
    title: req.body.title,
    content: req.body.content,
    deadline: req.body.deadline,
    // If there is no deadline value, "TypeError: Can not read property 'deadline' of undefined" occurs.
  });

vue表单组件

    create: function() {
      if (!this.todo.deadline) {
        this.todo.deadline = ''
        // not working
      }
      this.$http.post('/api/posts/create', this.post)
      .then(
        (response) => {
        ...
      })
    }

  let post = new Post({
    title: req.body.title?req.body.title:null,
    content: req.body.content?req.body.content:null,
    deadline: req.body.deadline?req.body.deadline:null,
  });

尝试此

最新更新