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);
});
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.
There is another way - use findByPk static method and update not-static method together. For example:
Note this code must be inside an asynchronous function.
Update function of sequelize returns a number of affected rows (first parameter of result array).
You should call find to get updated row
You can just find the item and update its properties and then save it. The save() results in a UPDATE query to the db
On Postgres:
Here's what I think you're looking for.
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 elementy
is the actual affected rows (only supported in postgres withoptions.returning
set totrue
.)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. Andplain: true
is just to return the object itself and not the other messy meta data that might not be useful.same thing you can do with async-await, especially to avoid nested Promises You just need to create async function :)
If you're using postgres and updating one row.