这个问题已经被提出,但是不能解决我的问题。
我的database
中有两个字段,分别是fromDate
和toDate
。我要在插入新数据时验证fromDate
和toDate
。
我的意思是,我已经有 fromDate = 2019-04-20 和 toDate = 2019-04-30 我的database
中的“ strong>”。
现在正尝试将 fromDate
插入为 2019-04-25
,将 toDate
插入为 2019-04-26
。
在这种情况下,会抛出类似的错误。
这是我的代码:
controller.js
const db = require('../../config/database');
const CubbersClosure = require('../../model/cubber/cubbersClosure.model')(db.sequelize, db.Sequelize);
exports.cubbersAddClosure = (req, res) =>{
CubbersClosure.create({
cubbersId:req.body.cubbersId,
userId:req.body.userId,
fromDate:req.body.fromDate,
toDate:req.body.toDate,
status:1,
createdBy:req.body.createdBy,
updatedBy:req.body.updatedBy
}).then(closure=>{
res.status(200).send({status: 'success', resCode:200, msg:'Shop closure date added successfully...!', data:closure});
}).catch(function (error) {
res.status(403).send({status: 'error', resCode:200, msg:'Internal Server Error...!', data:error});
});
}
model.js
module.exports = (sequelize, Sequelize)=>{
const cubbersAvailability = sequelize.define('cubbersclosure',{
closureId:{
type: Sequelize.INTEGER,
primaryKey:true,
autoIncrement:true
},
cubbersId:{type: Sequelize.INTEGER},
userId:{type: Sequelize.INTEGER},
fromDate:{type: Sequelize.STRING},
toDate:{type: Sequelize.STRING},
status:{type: Sequelize.INTEGER},
createdAt:{type: Sequelize.DATE},
createdBy:{type: Sequelize.INTEGER},
updatedAt:{type: Sequelize.DATE},
updatedBy:{type: Sequelize.INTEGER},
},
{
freezeTableName: true
});
return cubbersAvailability;
}
注意:在插入新日期之前,我要验证日期之间的此日期是否已经退出...!