0

I am inserting a user row into Postgres from Express.js like as follows:

db.none("INSERT INTO users (email, password) VALUES (${incoming.email}, crypt(${incoming.password}, gen_salt('bf')))",{
    incoming: { email: qemail, password: qpwd}
  }
).then(function (data) {
  // data is null
});

The email and password come from the query parameters on the React.js frontend. I use this query for the signup action on my application and wanted to know if I could get this newly added user back from this same query. I just felt that making another get request right after the signup to get the user info was inefficient, which I do for my login action. Thanks in advance.

2 Answers 2

1

Append RETURNING * to your query, and replace none with method one.

const row = await db.one("INSERT INTO users(email, password) VALUES(${incoming.email}, 
                          crypt(${incoming.password}, gen_salt('bf'))) RETURNING *",
    {
        incoming: { email: qemail, password: qpwd}
    }
);

// row = the newly inserted row
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Worked like a charm!
1

At the end of the query add RETURNING *. This should return the newly added query. (postgresql docs).

In your query:

"INSERT INTO users (
    email, password
  ) VALUES (
    ${incoming.email}, crypt(${incoming.password}, gen_salt('bf'))
  ) RETURNING *"

1 Comment

Thanks. Sorry I had to pick the other one as the answer due to more detail! I appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.