【发布时间】:2021-07-22 06:05:33
【问题描述】:
我有一个Session模型,它有日期、开始时间和结束时间,如下图 // 假设 Session 模型在同一个文件中定义 const mongoose = require('mongoose')
const SessionSchema = new mongoose.Schema({
date: {
type: Date,
required: [true, 'Date is required for session']
},
start: {
type: Date,
required: [true, 'Please specify end time for session']
},
end: {
type: Date,
required: [true, 'Please specify end time for session']
},
owner: { type: Schema.Types.ObjectId, ref: 'User' },
participants: {
type: [Schema.Types.ObjectId],
ref: 'User'
},
})
module.exports = mongoose.model('Session', SessionSchema)
在我的“/session-create”路由中,我想在创建会话之前确保所有者在会话时间间隔中没有另一个冲突部分。这意味着新会话的时间间隔应该在所有者的任何其他现有会话的时间间隔之外。
我该怎么做?我对如何确保在他们的时间间隔内没有冲突的会话感到困惑。
这是我的会话创建路线:
post("/create-session", (req, res) => {
const { date, start, end } = req.body
// Getting the start datetime
startHours = start.slice(0, 2)
startMinutes = start.slice(3)
startDateTime = new Date(date).setHours(startHours, startMinutes)
// Getting the end datetime
endHours = end.slice(0, 2)
endMinutes = end.slice(3)
endDateTime = new Date(date).setHours(endHours, endMinutes)
// Session owner cannot have 2 time-conflicting sessions
// Session start time should not in be in the
// time interval of already existing sessions, i.e,
// Session end time should not be within the time interval of already
// existing sessions
}
如何确保会话所有者在会话创建期间不能有 2 个时间冲突的会话?换句话说,我如何在创建时验证会话开始时间不在 已经存在会话的时间间隔并且会话结束时间不在已经存在会话的时间间隔内?
【问题讨论】:
标签: node.js express validation mongoose