2017-04-07 63 views
0

我试图调用此函数的Postgres core.get_age(BIGINT)在我queries.js在调用API节点

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" 
 
     }); 
 
    }) 
 
}

任何我在index.js路线一个Postgres数据库功能

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

当我在POSTMAN中致电http://localhost:8080/api/age/10时,我得到404错误。

出了什么问题?这是我想要调用Postgres函数的第一个实例()。只是想找回Postgres的表select * from this.this表的作品,但与功能我得到这个404

回答

0

始终使用.catch承诺!它会指出问题,就像您的案例中返回的行数无效。

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(); 
    }) 
} 
-1

唉唉..

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

,而不是一个我用它工作。 DB.ANY获胜。

+0

原因是你没有使用承诺的权利。必须始终提供'.catch'处理程序。这样你可以立即知道问题在哪里。 –