我正在使用Express和Sequelize JS构建一个简单的API。我试图用req.School
更新记录,但没有任何事情发生。我分享了下面的代码。我的输出也没有错误。你能救我吗?
控制器js
module.exports.params = function(req, res, next, id) {
School.find({
where: {
id: id
}
})
.then(function(school) {
if (!school) {
next(new Error("School not found by given ID"));
}else {
req.School = school;
next();
}
}, function(err) {
next(err);
});
};
module.exports.delete = function (req,res,next) {
req.School.Status = SysEnums.Deleted;
School.update(req.School,{
where:{
id:req.School.id
}
}).then(function (deleted) {
res.json(deleted);
});
};
路线JS
var router = require("express").Router();
var controller = require("../Controllers/SchoolController");
router.param("id",controller.params);
router.route("/").get(controller.get);
router.route("/:id").get(controller.getOne).delete(controller.delete);
module.exports = router;
答案 0 :(得分:0)
自params方法以来,req.School是一个续集对象。这是params方法。
module.exports.params = function(req, res, next, id) {
School.find({
where: {
id: id
}
})
.then(function(school) {
if (!school) {
next(new Error("School not found by given ID"));
}else {
req.School = school;
next();
}
}, function(err) {
next(err);
});
};
由于@Shivam说它是一个续集对象,我可以使用提供的方法。我想软删除我的记录,所以这是我的Sequelize JS的删除方法。
module.exports.delete = function(req, res, next) {
try {
req.School.Status = SysEnums.Deleted;
req.School.save().then(function(entity) {
res.json(entity);
});
} catch (e) {
console.log(e);
}
};
@PandhiBhaumik
感谢您的帮助。我将尝试使用构建方法作为第二个选项。