【发布时间】:2018-04-18 01:06:57
【问题描述】:
型号
const hoursSchema = new Schema({
date: {
type: Date,
required: true,
trim: true,
},
location: {
type: String,
required: true,
trim: true,
minlength: 1,
}
});
module.exports = mongoose.model('Hours', hoursSchema);
路线
router.post('/', (req, res) => {
const body = _.pick(req.body, ['date', 'location']);
const entry = new Hours(body);
res.send(entry);
// rest of code...
});
我提出一个发布请求,假设日期:12 / 日期:“12”。 Mongoose 将其视为时间戳,因为我在日期字段中收到此结果 - 1970-01-01T00:00:00.012Z。我怎样才能防止这种情况?当用户以 yyyy-mm-dd 或 dd-mm-yyyy 格式发送除日期以外的其他数据时,我想抛出错误
【问题讨论】:
-
可能是How to validate a date?的副本
标签: javascript node.js date express mongoose