-1

when i log data.id the result is undefined when i log data the result is

[ anonymous {

id: 23, username: 'vvv', password: '1', description: null, cutprice: null, salname: null, image: null, counter: 0 } ]

app.post('/api/hairdresser/signin', (req, res) => {
 console.log(req.body)
  connection.connect();
 var name = req.body.name;
 var password = req.body.password
  db.any('SELECT * FROM hairdressers where username = $1 ', 
 name).then(function(data) {
     console.log(data);
    console.log(data.id);
   }).catch(function(error) {

console.log(error);

 });
})
jadlmir
  • 465
  • 1
  • 6
  • 16

1 Answers1

2

Method any is documented to resolve with an array of rows, so data[0].id is the value you are looking for.

Most likely what you need in this scenario though, is method oneOrNone, which resolves with the row-object, or null when none returned:

db.oneOrNone('SELECT * FROM hairdressers where username = $1', name)
  .then(data => {
     // data = row object, or null when user not found;
     console.log(data && data.id);
  })
  .catch(error => {
      // more than 1 user found, or a different error
  });
vitaly-t
  • 24,279
  • 15
  • 116
  • 138