简体   繁体   中英

Sequelize MySQL update value copy from other table

I'm trying to make a controller that will do something like this:

UPDATE bankapplication_accounts
SET last_successful_logged = last_present_logged
WHERE id = 1

My controller in sequelize looks like this:

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

Can this controller write better?

There are 2 ways

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM