Correct way to deal with MySqlRawQueryResult

What is best practice to check if the data was created and how to get the newly added data?
db.insert(user).values({
firstname: data.data.firstname,
lastname: data.data.lastname,
username: data.data.username,
email: data.data.email,
password: hash
}).then((newUser) => {
//How do i check if the user was created?
}).catch((err) => {
return res.status(500).json(err)
})
db.insert(user).values({
firstname: data.data.firstname,
lastname: data.data.lastname,
username: data.data.username,
email: data.data.email,
password: hash
}).then((newUser) => {
//How do i check if the user was created?
}).catch((err) => {
return res.status(500).json(err)
})
5 Replies
Angelelz
Angelelz17mo ago
What is your dialect? Mysql?
Cambaru
CambaruOP17mo ago
Mysql / MariaDB @angelelz
Angelelz
Angelelz17mo ago
I believe for insert it would be newUser[0].affectedRows === 1
Cambaru
CambaruOP17mo ago
and afterwards i can just get it by const user = newUser[0] ?
Angelelz
Angelelz17mo ago
No, you either use the data you used in the insert statement or do a select afterwards Mysql doesn't return the date you just inserted
Want results from more Discord servers?
Add your server