简体   繁体   中英

Sequelize - update query with returning: true succeeds but returns undefined

I have the following function which I use to update the URL to the user's profile pic -

const updateProfilePic = async (req, res) => {
  const userId = req.param("id");
  if (userId) {
    const targetPath = `...`;
    User.update(
      {
        profilePic: targetPath
      },
      { where: { id: userId }, returning: true }
    )
      .then(updatedUser => {
        console.log(updatedUser);
        res.json(updatedUser);
      })
      .catch(error => {
        console.log(error);
        res.status(400).send({ error: error });
      });
  }
};

This updates the DB successfully - however then obtains [ undefined, 1 ] as the updatedUser instead of the user data - if I remove the returning: true flag, it just returns [ 1 ] . I'm not sure what I'm doing wrong - I'm trying to obtain the user object so that I can pass it on to the client.

I presume you are not using Postgres.

The returning property in options is only supported in Postgres .

Update() returns an array with one or two elements. The first element is the number of affected rows. The second array element is only supported in postgres and will return the updated row.

So you are getting the 1 returned which is the number of rows updated.

Docs

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