Sequelize - update record, and return result(Sequelize - 更新记录,并返回结果)
问题描述
我正在使用 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 - 更新记录,并返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Sequelize - 更新记录,并返回结果
基础教程推荐
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
