javascript - Sequelize - 更新查询并返回 : true succeeds but returns undefined

标签 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 });
      });
  }
};

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

最佳答案

我猜你没有使用 Postgres。

选项中的returning属性仅在Postgres中受支持

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

因此,您将返回 1,这是更新的行数。

Docs

关于javascript - Sequelize - 更新查询并返回 : true succeeds but returns undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58883687/

相关文章:

php - 更改日期格式

node.js - 我无法过滤也无法聚合通过 LogStash 保存到 ElasticSearch 的文档

node.js - 我如何等待 exec 进程在 Jest 中完成?

javascript - 在新打开的选项卡中运行脚本

mysql - 使用来自另一个表的另一个字段的值更新表中的字段

sql - 使用 Perl SQLite 自动递增

node.js - JSLint 和 ESLint 问题(括号)

javascript - 当状态更新时,我失去了对动态创建的 jsx 输入的关注

javascript - Angularjs - 将 $scope 传递给 Controller

javascript - session 超时 - 结果以分钟为单位