ultimately I am trying to call a callback function if my query is successfully inserted. I was thinking of trying to get the inserted row as a result and then test if the result exists and if it matches the inserted values then it was successful -> call my cb function. I'm getting an error that there's something wrong with my query. Is there a better query for this?
var username = user.username;
var password = user.password;
var email = user.email;
client.query(
'INSERT INTO account (username, password, email) OUTPUT
INSERTED.username VALUES($1, $2, $3)',
[username, password, email], (err, res) =>{
if (err) {
console.log(err);
cb(err);
}
console.log(res); // this is undefined how do i test if it was inserted?
client.end();
});
the error that is thrown is "syntax error at or near \"OUTPUT\"
That is an invalid syntax for Postgres Insert to return a value. NO such thing as OUTPUT. Try
insert into account (username, password, email)
values($1, $2, $3) returning username;
But really why brother? If the insert doesn't work you can relay on Postgres to raise an exception; if a comparison of the returned value fails, where's the error? In the insert or the data sent, or data result compared to.