const createUser = (username, firstname, lastname, address, password, cb) => {
connection.query(
`INSERT INTO users(username, firstname, lastname,address, password)
VALUES($1,$2,$3,$4,$5)`,
[username, firstname, lastname, address, password],
(error, result) => {
if (error) {
cb(error);
}
connection.query(
`select id from users where username=$1`,
[username],
(error, result) => {
if (error) {
cb(error);
} else {
cb(null, result.rows);
}
}
);
}
);
};
In the above query you're inserting a user into the database and then getting that user's id and returning it. Postgres has inbuilt functionality for this with RETURNING id:
const createUser = (username, firstname, lastname, address, password, cb) => {
connection.query(
`INSERT INTO users(username, firstname, lastname,address, password)
VALUES($1,$2,$3,$4,$5) RETURNING id`, ...
...
In the above query you're inserting a user into the database and then getting that user's id and returning it. Postgres has inbuilt functionality for this with
RETURNING id: