我正在使用 MySQL 的 sequelize.例如,如果我这样做:
I am using sequelize with MySQL. For example if I do:
models.People.update({OwnerId: peopleInfo.newuser},
{where: {id: peopleInfo.scenario.id}})
.then(function (result) {
response(result).code(200);
}).catch(function (err) {
request.server.log(['error'], err.stack);
).code(200);
});
无论人员模型是否成功更新,我都无法获取信息.变量 result 只是一个只有一个元素的数组,0=1
I am not getting information back if the people model was succesfully updated or not. Variable result is just an array with one element, 0=1
如何确定记录是否已更新.
How can I know for certain that the record was updated or not.
这就是我认为您正在寻找的.
Here's what I think you're looking for.
db.connections.update({
user: data.username,
chatroomID: data.chatroomID
}, {
where: { socketID: socket.id },
returning: true,
plain: true
})
.then(function (result) {
console.log(result);
// result = [x] or [x, y]
// [x] if you're not using Postgres
// [x, y] if you are using Postgres
});
来自 Sequelize docs:承诺返回一个包含一个或两个元素的数组.第一个元素 x
始终是受影响的行数,而第二个元素 y
是实际受影响的行(仅在带有 options.returning的 postgres 中支持code> 设置为
true
.)
From Sequelize docs:
The promise returns an array with one or two elements. The first element x
is always the number of affected rows, while the second element y
is the actual affected rows (only supported in postgres with options.returning
set to true
.)
假设您使用的是 Postgres,您可以使用 result[1].dataValues
访问更新后的对象.
Assuming you are using Postgres, you can access the updated object with result[1].dataValues
.
您必须设置 returning: true
选项来告诉 Sequelize 返回对象.而 plain: true
只是返回对象本身,而不是返回其他可能没用的杂乱元数据.
You must set returning: true
option to tell Sequelize to return the object. And plain: true
is just to return the object itself and not the other messy meta data that might not be useful.
这篇关于Sequelize - 更新记录,并返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!