Sequelize - update record, and return result
Update function of sequelize returns a number of affected rows (first parameter of result array).
You should call find to get updated row
models.People.update({OwnerId: peopleInfo.newuser},
{where: {id: peopleInfo.scenario.id}})
.then(() => {return models.People.findById(peopleInfo.scenario.id)})
.then((user) => response(user).code(200))
.catch((err) => {
request.server.log(['error'], err.stack);
});
You can just find the item and update its properties and then save it. The save() results in a UPDATE query to the db
const job = await Job.findOne({where: {id, ownerId: req.user.id}});
if (!job) {
throw Error(`Job not updated. id: ${id}`);
}
job.name = input.name;
job.payload = input.payload;
await job.save();
On Postgres:
Executing (default): UPDATE "jobs" SET "payload"=$1,"updatedAt"=$2 WHERE "id" = $3
Finally i got it. returning true wont work in mysql , we have to use findByPk in order hope this code will help.
return new Promise(function(resolve, reject) {
User.update({
subject: params.firstName, body: params.lastName, status: params.status
},{
returning:true,
where: {id:id }
}).then(function(){
let response = User.findById(params.userId);
resolve(response);
});
});
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
});
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
.)
Assuming you are using Postgres, you can access the updated object with result[1].dataValues
.
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.