Sergio
Sergio

Reputation: 31

Turn data from PostgreSQL to JavaScript variables

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.

Upvotes: 2

Views: 730

Answers (2)

Aalexander
Aalexander

Reputation: 5004

You can get it by this

result.rows[0].pword

Upvotes: 0

infecting
infecting

Reputation: 763

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

Upvotes: 1

Related Questions