我正在使用VueJS,ExpressJS和MongoDB创建CRUD。
在Mongoose中,我有一个截止日期:{Type:Date,可选:true},但是如果我不向Vue的输入发送截止日期值,则会在Express的Create API中出现错误。
如果我不指定日期值,我想生成一个NULL。
我该怎么办?
mongoose.js
...
deadline: {
type: Date,
optional: true
},
routes.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) => {
...
})
}
答案 0 :(得分:0)
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,
});
尝试