所以我正在预订系统,或尝试。我有一个start_block和end_block,它表示当天的可预留时间。
我的架构如下:
var ReservationSchema = new Schema({
rid: {type: Number},
day: {type: Date},
start_block: {type: Number},
end_block: {type: Number},
kind: {type: String},
agent: {type: Schema.Types.ObjectId, ref: 'User'},
primary_client: {type: Schema.Types.ObjectId, ref: 'User'},
secondary_clients: [{type: Schema.Types.ObjectId, ref: 'User'}],
created_date: {type: Date, default: Date.now},
updated_date: {type: Date, default: Date.now}
});
我的验证如下:
ReservationSchema.pre("validate", function (next, done) {
var self = this;
console.log("being validated");
mongoose.models["Reservation"]
.findOne({
$or: [
{start_block: {$gte: self.start_block, $lte: self.end_block}, rid: self.rid},
{end_block: {$gte: self.start_block, $lte: self.end_block}, rid: self.rid}
]
}, function (err, user) {
if (err) {
console.log(err);
done(err);
} else if (user) {
console.log("blocked!!!!");
self.invalidate("start_block", "Times overlap");
done(new Error("Times Slots Cannot Overlap"));
} else {
console.log("passed");
done();
}
});
next();
});
我的种类是:
Reservation.find({}).remove(function(){
User.find({}).limit(1)
.exec(function (err, result) {
if(err) throw err;
var person = result[0];
if(person != null){
for(var i = 0; i <= 25; i++){
var start = Math.floor(Math.random() * 31) + 1;
Reservation.create({
rid: Math.floor(Math.random() * 6) + 1,
day: new Date("1985-10-06"),
start_block: start,
end_block: start + Math.floor(Math.random() * 1) + 2,
agent: person._id,
primary_client: person._id,
kind: "Reservation"
});
}
}
}
);
});
播种时我的一部分输出记录,各种疯狂的控制台日志...... 正在验证
{ [CastError: Cast to number failed for value "undefined" at path "end_block"]
message: 'Cast to number failed for value "undefined" at path "end_block"',
name: 'CastError',
type: 'number',
value: undefined,
path: 'end_block' }
being validated
being validated
{ [CastError: Cast to number failed for value "undefined" at path "end_block"]
message: 'Cast to number failed for value "undefined" at path "end_block"',
name: 'CastError',
type: 'number',
value: undefined,
path: 'end_block' }
passed
passed
最后,结果是,当放在基于div的桌子上时,预订仍然重叠...试图在相当长的几个小时内得到这个并且谷歌搜索失败让我在这里希望有更聪明的头脑。谢谢!
编辑:清除额外的空白创建,清理日志。但仍然无法弄清楚为什么每个条件都没有找到重叠的槽。必须有一个逻辑问题。我已经把它写出来了,没有看到它......
这是他重叠的样子......
预订19与其他两个预订重叠
我已经100次重写了我的规则,这是他们现在的样子......
.findOne({
$or: [
{start_block: {$gte: self.start_block, $lt: self.end_block}, rid: self.rid},
{start_block: {$gt: self.start_block, $lte: self.end_block}, rid: self.rid},
{end_block: {$lte: self.start_block, $gt: self.end_block}, rid: self.rid},
{end_block: {$lt: self.start_block, $gte: self.end_block}, rid: self.rid}
]
}, function (err, user) {
if (err) {
console.log('error)');
console.log(err);
done(err);
} else if (user) {
console.log("blocked!!!!");
self.invalidate("start_block", "Times overlap");
done(new Error("Times Slots Cannot Overlap"));
} else {
console.log("passed");
done();
}
答案 0 :(得分:0)
我不确定为什么会这样,但您将一个空的{}
对象作为第二个参数传递给Reservation.create
来电。这将被视为要创建的第二个doc,但它没有验证器所期望的字段。
所以删除第二个参数,以便调用看起来像这样:
Reservation.create({
rid: Math.floor(Math.random() * 6) + 1,
day: new Date("1985-10-06"),
start_block: start,
end_block: start + Math.floor(Math.random() * 1) + 2,
agent: person._id,
primary_client: person._id,
kind: "Reservation"
});