I want to get the image field
of the updated row so i can send it to the client, it works when using SELECT
but when using UPDATE
it only returns some information about the process like affectedRows
, insertId
..etc:
const user = await DB.execute(
"UPDATE `users` SET `email` = ?",[email]
);
console.log(user[0].image);
result of console.log(user);
:
[
ResultSetHeader {
fieldCount: 0,
affectedRows: 1,
insertId: 0,
info: 'Rows matched: 1 Changed: 0 Warnings: 0',
serverStatus: 2,
warningStatus: 0,
changedRows: 0
},
undefined
]
You certainly need to perform a SELECT
query after the UPDATE
operation to fetch the updated data.
NB: (MySQL 8.0.21 and above)
, I have also not tested it but in theory it should work, You need to return the image immediately after update.
Here is a code snippet to guide you.
const [rows] = await DB.execute(
"UPDATE `users` SET `email` = ? WHERE `id` = ? RETURNING `image`", [email, userId]
);
if (rows.length > 0) {
const image = rows[0].image;
console.log(image); // gets the role
}
Alternatively You can try and do this
await DB.execute(
"UPDATE `users` SET `email` = ? WHERE `id` = ?", [email, userId]
);
// after updating the user column find the image
const [rows] = await DB.execute(
"SELECT `image` FROM `users` WHERE `id` = ?", [userId]
);
if (rows.length > 0) {
const image = rows[0].image;
console.log(image);
}