0

I am trying to call this Postgres function core.get_age(bigint) in my queries.js

function getAge(req, res, next){
  var customer_id= parseInt(req.params.customer_id);
  db.one('SELECT * FROM core.get_age($1)', customer_id)
    .then(function(data){
      res.status(200)
        .json({
          status: 'success',
          data : data,
          message: "Retrieved Age"
        });
    })
}

Any my routes in index.js

router.get('/api/age/:customer_id', db.getAge);

When i call http://localhost:8080/api/age/10 in POSTMAN i get 404 error.

What is wrong? This is the first instance i am trying to call a Postgres function(). Just trying to retrieve Postgres table with select * from this.this table works but with function I am getting this 404.

OLDMONK
  • 382
  • 2
  • 7
  • 25

2 Answers2

0

Always use .catch with promises! It will point at the problem, like the invalid number of rows returned in your case.

function getAge(req, res, next) {
  db.func('core.get_age', +req.params.customer_id)
    .then(data => {
      res.status(200)
        .json({
          status: 'success',
          data : data,
          message: "Retrieved Age"
        });
    })
    .catch(error => {
        console.log(error);
        next();
    })
}
vitaly-t
  • 24,279
  • 15
  • 116
  • 138
-1

Ahhh..

db.any('SELECT * FROM core.get_age($1)', customer_id)

instead of one which I used worked. DB.ANY wins.

OLDMONK
  • 382
  • 2
  • 7
  • 25
  • The reason is that you do not use promises right. One must always provide a `.catch` handler. That way you would immediately know where the issue is. – vitaly-t Apr 07 '17 at 21:10