我有以下函数,用于更新用户个人资料图片的 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,这是更新的行数。
关于javascript - Sequelize - 更新查询并返回 : true succeeds but returns undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58883687/