2

I have the following code:

    const result = await client.query("select pword from users where username like '" + ln + "';");
    console.log(result.rows)

which sends following to the terminal: [ { pword: '00' } ]

Is there a way for me to extract '00', and store it in a variable? I am using Node JS and Express.

2

2 Answers 2

1

The postgres query is returning an array. To access the contents of this array you are going to get the first index by doing result[0]. Next you are going to access the variable in the object nested by the array using dot notation. So your final will be result[0].pword

you can store this in a variable by doing

var foo = result[0].pword

it looks like you could use some information about arrays and how to use them. Here are some relevant docs

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array https://www.w3schools.com/js/js_arrays.asp

Sign up to request clarification or add additional context in comments.

Comments

0

You can get it by this

result.rows[0].pword

Comments

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.