Sequelize更新查询并返回:true成功,但返回undefined



我有以下功能,用于更新用户配置文件图片的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 });
});
}
};

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

我想您没有使用Postgres。

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

CCD_ 7返回一个包含一个或两个元素的数组。第一个元素是受影响的行数。第二个数组元素仅在postgres中受支持,并且将返回更新后的行。

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

文档

最新更新