gpt4 book ai didi

javascript - Sequelize MySQL更新值从其他表复制

转载 作者:行者123 更新时间:2023-11-30 21:35:30 24 4
gpt4 key购买 nike

我正在尝试制作一个可以执行以下操作的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com