我正在尝试制作一个可以执行以下操作的 Controller :
UPDATE bankapplication_accounts
SET last_successful_logged = last_present_logged
WHERE id = 1
我在 sequelize 中的 Controller 看起来像这样:
exports.updateLastLoggedDate = (req, res) => {
User.findOne({
where: {
id: req.params.userId,
},
}).then(user => {
if (user) {
User.update(
{
last_successfull_logged: user.last_present_logged,
},
{ where: { id: req.params.userId } },
).then(() => {
res.status(200).send('logout correct');
});
}
});
};
这个controller能写的更好吗?
最佳答案
有两种方式
1:
exports.updateLastLoggedDate = (req, res) => {
User.findOne({
where: {
id: req.params.userId,
},
}).then((user) => {
if (user) {
user.update({
last_successfull_logged: user.last_present_logged,
}).then(() => {
res.status(200).send("logout correct");
});
}
});
};
2:
User.update(
{
last_successfull_logged: user.last_present_logged,
}, /* set attributes' value */
{
where: {
id: req.params.userId,
},
}, /* where criteria */
).then(() => {
res.status(200).send("logout correct");
});
关于javascript - Sequelize MySQL更新值从其他表复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54048583/