Skip to content
Advertisement

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.

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement