Sequelize - 更新查询并返回:true 成功但返回未定义

Alk*_*Alk 3 javascript sql node.js express sequelize.js

我有以下功能,用于更新用户个人资料图片的 URL -

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 });
      });
  }
};
Run Code Online (Sandbox Code Playgroud)

这将成功更新数据库 - 但是作为用户数据而不是then获取- 如果我删除该标志,它只会返回. 我不确定我做错了什么 - 我正在尝试获取用户对象,以便我可以将其传递给客户端。[ undefined, 1 ]updatedUserreturning : true[ 1 ]

Sam*_*l G 6

我想你没有使用 Postgres。

returningoptions 中的属性仅在 Postgres 中受支持

Update()返回一个包含一两个元素的数组。第一个元素是受影响的行数。第二个数组元素仅在 postgres 中受支持,并将返回更新的行。

所以你得到的1是更新的行数。

文档