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

标签 javascript mysql sequelize.js

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

相关文章:

php - 使用 XAMPP 从/opt/lampp/目录的副本恢复 MediaWiki wiki

php - 上传半个文件?

javascript - 将视频替换为错误的图像

javascript - 未捕获的语法错误 : Invalid destructuring assignment target

javascript - 在 Sequelize 中模拟

mysql - 如何在 Sequelize 中实现聚类?

node.js - 从 Promise Sequelize 返回值

javascript - 这个 javascript 响应函数有什么作用?

php - 如何使用 FluentPDO 连接列?

php - 使用 PHP 识别特定值范围内的行数