sequelize mysql更新值从其他表复制

izj3ouym  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(314)

我正在尝试制作一个控制器,它将执行以下操作:

UPDATE bankapplication_accounts
SET last_successful_logged = last_present_logged
WHERE id = 1

sequelize中的控制器如下所示:

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');
      });
    }
  });
};

这个控制器能写得更好吗?

kpbpu008

kpbpu0081#

有两种方法
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");
  });

相关问题